200 lines
7.3 KiB
Objective-C
Executable File
200 lines
7.3 KiB
Objective-C
Executable File
//
|
|
// MTCommonListTableViewController.m
|
|
// MTCommonListTable
|
|
//
|
|
// Created by ko1o on 2018/8/29.
|
|
// Copyright © 2018年 ko1o. All rights reserved.
|
|
//
|
|
|
|
#import "MTCommonListTableViewController.h"
|
|
#import "MTCommonListTableConfig.h"
|
|
#import "MTCommonListTableViewCell.h"
|
|
|
|
@interface MTCommonListTableViewController ()
|
|
|
|
@property (nonatomic, strong) MTCommonListTableConfig *tableConfig;
|
|
|
|
@end
|
|
|
|
@implementation MTCommonListTableViewController
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
|
|
[self setupCommonListTableViewUI];
|
|
}
|
|
|
|
- (MTCommonListTableConfig *)tableConfigForTableViewContorller {
|
|
NSAssert(NO, @"MTError: 子类需要实现该协议方法!!!");
|
|
return nil;
|
|
}
|
|
|
|
- (void)setupCommonListTableViewUI
|
|
{
|
|
self.tableConfig = [self tableConfigForTableViewContorller];
|
|
MTCommonListTableConfig *tableConfig = self.tableConfig;
|
|
self.title = tableConfig.tableTitle;
|
|
if (!self.tableView) {
|
|
self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:tableConfig.tableStyle];
|
|
self.tableView.delegate = self;
|
|
self.tableView.dataSource = self;
|
|
self.view = self.tableView;
|
|
}
|
|
self.tableView.backgroundColor = tableConfig.tableBackgroundColor;
|
|
self.tableView.separatorStyle = tableConfig.separatorStyle;
|
|
self.tableView.contentInset = tableConfig.tableContentInset;
|
|
|
|
|
|
if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 11.0) {
|
|
self.tableView.contentInset = UIEdgeInsetsMake(NAVIGATION_BAR_HEIGHT, 0, 0, 0);
|
|
}
|
|
}
|
|
|
|
- (void)reloadData
|
|
{
|
|
[self setupCommonListTableViewUI];
|
|
[self.tableView reloadData];
|
|
}
|
|
|
|
#pragma mark - Table view data source
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
{
|
|
return self.tableConfig.sectionConfigs.count;
|
|
}
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
{
|
|
NSArray *cellConfigs = ((MTCommonListTableSectionConfig *)self.tableConfig.sectionConfigs[section]).cellConfigs;
|
|
return cellConfigs.count;
|
|
}
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
MTCommonListTableSectionConfig *sectionConfig = self.tableConfig.sectionConfigs[indexPath.section];
|
|
MTCommonListTableCellConfig *cellConfig = sectionConfig.cellConfigs[indexPath.row];
|
|
|
|
MTCommonListTableViewCell *cell = [MTCommonListTableViewCell cellWithConfig:cellConfig];
|
|
|
|
return cell;
|
|
}
|
|
|
|
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
|
|
{
|
|
MTCommonListTableSectionConfig *sectionConfig = self.tableConfig.sectionConfigs[section];
|
|
|
|
if (sectionConfig.headerView) return sectionConfig.headerView;
|
|
|
|
UIView *headerView = [[UIView alloc] init];
|
|
headerView.backgroundColor = [UIColor clearColor];
|
|
if (!sectionConfig.headerTitle && !sectionConfig.headerAttributedTitle) return headerView;
|
|
|
|
UILabel *headerLabel = [[UILabel alloc] init];
|
|
headerLabel.userInteractionEnabled = YES;
|
|
headerLabel.numberOfLines = 0;
|
|
if (sectionConfig.headerAttributedTitle) {
|
|
headerLabel.attributedText = sectionConfig.headerAttributedTitle;
|
|
} else {
|
|
headerLabel.text = sectionConfig.headerTitle;
|
|
headerLabel.textColor = sectionConfig.headerTitleColor;
|
|
headerLabel.font = sectionConfig.headerTitleFont;
|
|
}
|
|
|
|
CGFloat width = self.view.bounds.size.width - sectionConfig.headerInset.left - sectionConfig.headerInset.right;
|
|
[headerLabel sizeToFit];
|
|
CGFloat height = headerLabel.frame.size.height;
|
|
headerLabel.frame = CGRectMake(sectionConfig.headerInset.left, sectionConfig.headerInset.top, width, height);
|
|
headerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, height+sectionConfig.headerInset.top+sectionConfig.headerInset.bottom);
|
|
[headerView addSubview:headerLabel];
|
|
|
|
return headerView;
|
|
}
|
|
|
|
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
|
|
{
|
|
|
|
MTCommonListTableSectionConfig *sectionConfig = self.tableConfig.sectionConfigs[section];
|
|
|
|
if (sectionConfig.footerView) return sectionConfig.footerView;
|
|
|
|
UIView *footerView = [[UIView alloc] init];
|
|
footerView.backgroundColor = [UIColor clearColor];
|
|
if (!sectionConfig.footerTitle && !sectionConfig.footerAttributedTitle) return footerView;
|
|
|
|
UILabel *footerLabel = [[UILabel alloc] init];
|
|
footerLabel.userInteractionEnabled = YES;
|
|
footerLabel.numberOfLines = 0;
|
|
if (sectionConfig.footerAttributedTitle) {
|
|
footerLabel.attributedText = sectionConfig.footerAttributedTitle;
|
|
} else {
|
|
footerLabel.text = sectionConfig.footerTitle;
|
|
footerLabel.textColor = sectionConfig.footerTitleColor;
|
|
footerLabel.font = sectionConfig.footerTitleFont;
|
|
}
|
|
|
|
CGFloat width = self.view.bounds.size.width - sectionConfig.footerInset.left - sectionConfig.footerInset.right;
|
|
[footerLabel sizeToFit];
|
|
CGFloat height = footerLabel.frame.size.height;
|
|
footerLabel.frame = CGRectMake(sectionConfig.footerInset.left, sectionConfig.footerInset.top, width, height);
|
|
footerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, footerLabel.frame.size.height +sectionConfig.footerInset.top+sectionConfig.footerInset.bottom);
|
|
[footerView addSubview:footerLabel];
|
|
|
|
return footerView;
|
|
}
|
|
|
|
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
|
|
{
|
|
id titleForHeader = self.tableConfig.sectionHeaderTitles[section];
|
|
if ([titleForHeader isKindOfClass:[MTNullElement class]]) {
|
|
return nil;
|
|
}
|
|
return titleForHeader;
|
|
}
|
|
|
|
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
|
|
{
|
|
id titleForFooter = self.tableConfig.sectionFooterTitles[section];
|
|
if ([titleForFooter isKindOfClass:[MTNullElement class]]) {
|
|
return nil;
|
|
}
|
|
return titleForFooter;
|
|
}
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
MTCommonListTableSectionConfig *sectionConfig = self.tableConfig.sectionConfigs[indexPath.section];
|
|
MTCommonListTableCellConfig *cellConfig = sectionConfig.cellConfigs[indexPath.row];
|
|
return cellConfig.height;
|
|
}
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
|
|
{
|
|
MTCommonListTableSectionConfig *sectionConfig = self.tableConfig.sectionConfigs[section];
|
|
return sectionConfig.headerHeight;
|
|
}
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
|
|
{
|
|
MTCommonListTableSectionConfig *sectionConfig = self.tableConfig.sectionConfigs[section];
|
|
return sectionConfig.footerHeight;
|
|
}
|
|
|
|
#pragma mark - Table view delegate
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
MTCommonListTableSectionConfig *sectionConfig = self.tableConfig.sectionConfigs[indexPath.section];
|
|
MTCommonListTableCellConfig *cellConfig = sectionConfig.cellConfigs[indexPath.row];
|
|
|
|
if (cellConfig.cellAction) {
|
|
cellConfig.cellAction(self, indexPath, cellConfig);
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
return;
|
|
}
|
|
if (self.tableConfig.cellAction) {
|
|
self.tableConfig.cellAction(self, indexPath, cellConfig);
|
|
}
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
}
|
|
|
|
@end
|