BINTableViewHandle
让控制器远离代理
最大的好处:之前项目三百多个类,采用一个控制器绑定一个cell类,感觉太繁琐和低效,所以有了此类的产生.
所有的代理方法都走这2个类,如果需要特殊定制的部分,从外部传入即可.tableView属于控制器,但是代理属于着2个类,cell也可外部传入,cell作为高复用组件各种cell自由组合.
(从此不用再繁琐配置UITableView视图了,适用范围:所有列表)
#import "BINTableViewHandle.h"
用于单sections列表数据展示(一个cell一个数据模型)
#import "BINTableViewHandleSections.h"
用于多sections列表数据展示(一个section一个数据模型)
BINTableViewHandle.h使用
-(void)createTableView{
UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小
tableView.backgroundColor = [UIColor yellowColor];
self.tableView = tableView;
[self.view addSubview:self.tableView];
}
- (void)setupTableView
{
CellHeightBlock heightBlock = ^CGFloat(NSIndexPath *indexPath, id item) {
return [self getCellHeightIndexPath:indexPath item:item];
};
ReturnCellBlock cellBlock = ^UITableViewCell*(UITableView * tableView,NSIndexPath * indexPath,id item){
return [self getCellWithTableView:tableView indexPath:indexPath item:item];
};
DidSelectCellBlock selectedBlock = ^(NSIndexPath *indexPath, id item) {
NSLog(@"click row : %@",@(indexPath.row)) ;
[self goMessageListWithIndexPath:indexPath];
};
// NSLog(@"list : %@",self.titleMarr);
self.tableHandler = [[BINTableViewHandle alloc] initWithItems:self.titleMarr
cellHeightBlock:heightBlock
returnCellBlock:cellBlock
didSelectBlock:selectedBlock] ;
[self.tableHandler handleTableViewDatasourceAndDelegate:self.tableView] ;
}
- (CGFloat)getCellHeightIndexPath:(NSIndexPath *)indexPath item:(id)item{
return 60;
}
- (UITableViewCell *)getCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath item:(id)item{
BINConmmonDataModel * dataModel = self.titleMarr[indexPath.row];
BINTableViewZeroCell * cell = [BINTableViewZeroCell cellWithTableView:tableView];
// BINConmmonDataModel * dataModel = (BINConmmonDataModel *)item;
cell.imgViewLeft.image = [UIImage imageNamed: dataModel.photoUrl];
cell.labelLeft.text = [NSString stringWithFormat:@"%@",dataModel.name];
cell.labelMark.text = [NSString stringWithFormat:@"%@",dataModel.orderCount];
cell.labelLeftSub.text = [NSString stringWithFormat:@"%@",dataModel.mobilePhone];
cell.labelMarkSub.text = [NSString stringWithFormat:@"%@",dataModel.markTips];
return cell ;
}
- (void)goMessageListWithIndexPath:(NSIndexPath *)indexPath {
//详情页
}
BINTableViewHandleSections.h使用
-(void)createTableView{
UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds, CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小
tableView.backgroundColor = [UIColor yellowColor];
self.tableView = tableView;
[self.view addSubview:self.tableView];
}
- (void)setupTableView
{
CellHeightBlock heightBlock = ^CGFloat(NSIndexPath *indexPath, id item) {
return [self getCellHeightIndexPath:indexPath item:item];
};
ReturnCellBlock cellBlock = ^UITableViewCell*(UITableView * tableView,NSIndexPath * indexPath,id item){
return [self getCellWithTableView:tableView indexPath:indexPath item:item];
};
DidSelectCellBlock selectedBlock = ^(NSIndexPath *indexPath, id item) {
NSLog(@"click row : %@",@(indexPath.row)) ;
[self goMessageListWithIndexPath:indexPath];
};
// NSLog(@"list : %@",self.titleMarr);
self.tableHandler = [[BINTableViewHandleSections alloc] initWithItems:self.titleMarr
cellHeightBlock:heightBlock
returnCellBlock:cellBlock
didSelectBlock:selectedBlock] ;
self.tableHandler.sectionCount = 3;
[self.tableHandler handleTableViewDatasourceAndDelegate:self.tableView] ;
//sectionHeader配置(options)
ReturnSectionViewHeightBlock sectionViewHeightBlock = ^CGFloat(NSInteger section, BOOL isHeader){
return [self getSectionHeight:section isHeader:isHeader];
};
self.tableHandler.returnSectionViewHeightBlock = sectionViewHeightBlock;
ReturnSectionViewBlock sectionViewBlock = ^UIView*(NSInteger section, BOOL isHeader){
return [self getSectionView:section isHeader:isHeader];
};
self.tableHandler.returnSectionViewBlock = sectionViewBlock;
}
- (CGFloat)getSectionHeight:(NSInteger)section isHeader:(BOOL)isHeader{
if (isHeader) {
return 10;
}
return 0;
}
- (UIView *)getSectionView:(NSInteger)section isHeader:(BOOL)isHeader{
UIView * backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), self.tableView.sectionFooterHeight)];
if (isHeader) {
return backgroudView;
}else{
return nil;
}
return backgroudView;
}
- (CGFloat)getCellHeightIndexPath:(NSIndexPath *)indexPath item:(id)item{
CGFloat height = 60;
switch (indexPath.row) {
case 0:
height = 60;
break;
case 1:
height = 80;
break;
case 2:
height = 50;
break;
default:
break;
}
return height;
}
- (UITableViewCell *)getCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath item:(id)item{
// NSArray *arr = [self getInfomation:self.titleMarr[indexPath.section] indexPath:indexPath];
BINConmmonDataModel * dataModel = self.titleMarr[indexPath.row];
switch (indexPath.row) {
case 0:
{
BINTableViewZeroCell * cell = [BINTableViewZeroCell cellWithTableView:tableView];
//
cell.imgViewLeft.hidden = YES;
cell.labelLeft.text = [NSString stringWithFormat:@"%@",dataModel.name];
return cell;
}
break;
case 1:
{
BINTableViewZeroCell * cell = [BINTableViewZeroCell cellWithTableView:tableView];
//
cell.imgViewLeft.image = [UIImage imageNamed: dataModel.photoUrl];
cell.labelLeft.text = [NSString stringWithFormat:@"%@",dataModel.name];
cell.labelMark.text = [NSString stringWithFormat:@"%@",dataModel.orderCount];
cell.labelLeftSub.text = [NSString stringWithFormat:@"%@",dataModel.mobilePhone];
cell.labelMarkSub.text = [NSString stringWithFormat:@"%@",dataModel.markTips];
return cell;
}
break;
case 2:
{
BINTableViewZeroCell * cell = [BINTableViewZeroCell cellWithTableView:tableView];
//
cell.imgViewLeft.hidden = YES;
cell.labelMark.text = [NSString stringWithFormat:@"%@",dataModel.orderCount];
cell.labelMarkSub.text = [NSString stringWithFormat:@"%@",dataModel.markTips];
return cell;
}
break;
default:
break;
}
return nil ;
}
- (void)goMessageListWithIndexPath:(NSIndexPath *)indexPath {
//详情页
}
备注:此处默认cell是固定高度,如需要实时计算,请自己缓存(思路:建议高度字典,indexPath的section和row为key,计算的高度为值,字典没有则计算并存到字典,有直接返回).
网友评论