手写继承NSObject类 此类实现TableView的DataSource和Delegate的协议。自己写个协议,在C中去进行委托传值。整体思路,就是把TableView的委托以拉链的形式,在管理类中实现。
代码如下
.h中的声明
#import <Foundation/Foundation.h>
@protocol MCSingleTableViewConfigerDataSource <NSObject>
@required
- (NSString *)cellIndentifier; ///<委托传递Indentifier注意Indentifier要和Cell的类名一致
- (NSMutableArray *)dataSourceArray;///<委托传递的数组
@end
@interface MCSingleTableViewConfiger :NSObject<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)id <MCSingleTableViewConfigerDataSource> singleDataSource;
@property (nonatomic,copy)void (^configureCellBlock)(UITableViewCell *cell,id model,NSIndexPath *indexPath);///<配置Cell的Block外部只要写Cell的Set方法即可,不用实例化Cell
@property (nonatomic,copy)CGFloat (^configureCellHeightBlock)(id model);///此Blcok为了接收高度的回调
- (instancetype)initAndConfigerTableView : (UITableView *)tableView;
@end
.m中的实现
#import "MCSingleTableViewConfiger.h"
@implementation MCSingleTableViewConfiger
- (instancetype)initAndConfigerTableView : (UITableView *)tableView
{
self = [superinit];
if (self) {
tableView.bounces = YES;
tableView.userInteractionEnabled = YES;
tableView.showsVerticalScrollIndicator =NO;
tableView.showsHorizontalScrollIndicator =NO;
tableView.separatorStyle =UITableViewCellSelectionStyleNone;
tableView.dataSource = self;
tableView.delegate = self;
}
return self;
}
#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.singleDataSourcedataSourceArray]count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 0.0f;
id model = [self.singleDataSourcedataSourceArray][[indexPath row]];
if (self.configureCellHeightBlock) {
returnself.configureCellHeightBlock (model);
}
return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:[self.singleDataSourcecellIndentifier]];
if ([[self.singleDataSourcedataSourceArray] count] ==0) {
return cell;
}
if (cell == nil) {
cell = [[NSClassFromString([self.singleDataSourcecellIndentifier])alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:[self.singleDataSourcecellIndentifier]];
}
id model = [self.singleDataSourcedataSourceArray][[indexPath row]];
if (self.configureCellBlock) {
self.configureCellBlock(cell, model, indexPath);
}
return cell;
}
Controller中使用
实现委托
#pragma mark - MCSingleTableViewConfigerDataSource
- (NSString *)cellIndentifier
{
return @"MCPublicCell";
}
- (NSMutableArray *)dataSourceArray
{
return self.dataArr;
}
初始化的时候就可以配置Cell了,代码少了很多,重复的不用在写。
//初始化Tableview
- (void)initTableView
{
self.userinfoTableView =[[UITableView alloc] initWithFrame:CGRectMake(0.0f, self.titleBar.bottom, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - self.titleBar.bottom) style:UITableViewStylePlain];
self.singleConfiger = [[MCSingleTableViewConfiger alloc] initAndConfigerTableView:self.userinfoTableView];
self.singleConfiger.singleDataSource = self;
self.singleConfiger.configureCellBlock = ^ (UITableViewCell *cell, id model, NSIndexPath *indexPath){
//在这里实现Cell的Set方法
[(MCPublicCell*)cell setMCPubicCellWithModel:model WithCellType:CellTypeNormal WithCellFocusBtnType:CellFocusBtnNeedHidden WithCellHeadBtnType:CellHeadBtnNeedHidden];
};
self.singleConfiger.configureCellHeightBlock = ^ (id model) {
//在这里实现Cell的高度回调 当然如果是定值就传死值,我项目中是需要动态计算的
MCPublicCellModel *tempModel = model;
return tempModel.cacheCellHight == 0.0f ? 0.1f : tempModel.cacheCellHight;
};
[self.view addSubview:self.userinfoTableView];