簡單封裝tableview把代理換成Block

直接上代碼了

LeBaseTabView.h里面


```#import@interface LeBaseTabView : UITableView/**```

row

*/

@property (nonatomic,copy)? NSInteger (^RowAtIndexPathBlock)(UITableView *tableView,NSInteger section);

//- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

/**

section

*/

@property (nonatomic,copy) NSInteger (^numberOfSectionsInTableViewBlock)(UITableView *tableView);

/**

返回UItabLeviewcell

*/

@property (nonatomic,copy) UITableViewCell * (^cellForRowAtIndexPathBlock)(UITableView *tableView,NSIndexPath *indexPath);

/**

heightForHeaderInSection

*/

@property (nonatomic,copy) CGFloat (^heightForHeaderInSectionBlock)(UITableView *tableView,NSInteger section);

/**

heightForFooterInSection

*/

@property (nonatomic,copy) CGFloat (^heightForFooterInSectionBlock)(UITableView *tableView,NSInteger section);

/**

didSelectRowAtIndexPath

*/

@property (nonatomic,copy) void (^didSelectRowAtIndexPathBlock)(UITableView *tableView,NSIndexPath * indexPath);

/**

viewForFooterInSection

*/

@property (nonatomic,copy) UIView * (^viewForFooterInSectionBlcok)(UITableView *tableView,NSInteger? section);

/**

viewForHeaderInSection

*/

@property (nonatomic,copy) UIView* (^viewForHeaderInSectionBlock)(UITableView *tableView,NSInteger section);

@end

LeBaseTabView.m里面實(shí)現(xiàn)

#import "LeBaseTabView.h"

@implementation LeBaseTabView

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

// Drawing code

}

*/

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{

if (self = [super initWithFrame:frame style:style]) {

self.delegate = self;

self.dataSource = self;

self.estimatedRowHeight = 200;

self.rowHeight = UITableViewAutomaticDimension;

self.separatorStyle = UITableViewCellSeparatorStyleNone;

}

return self;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (self.RowAtIndexPathBlock) {

return self.RowAtIndexPathBlock(tableView,section);

}else{

return 0;

}

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

if (self.numberOfSectionsInTableViewBlock) {

return self.numberOfSectionsInTableViewBlock(tableView);

}else{

return 1;

}

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

return self.cellForRowAtIndexPathBlock(tableView,indexPath);

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

if (self.heightForHeaderInSectionBlock) {

return self.heightForHeaderInSectionBlock(tableView,section);

}else{

return 0.01f;

}

}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

if (self.viewForFooterInSectionBlcok) {

return? self.viewForFooterInSectionBlcok(tableView,section);

}else{

return? (UIView*)[UIView new];

}

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

if (self.heightForFooterInSectionBlock) {

return self.heightForFooterInSectionBlock(tableView,section);

}else{

return 0.01f;

}

}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

if (self.viewForHeaderInSectionBlock) {

return? self.viewForHeaderInSectionBlock(tableView,section);

}else{

return? (UIView*)[UIView new];

}

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if (self.didSelectRowAtIndexPathBlock) {

self.didSelectRowAtIndexPathBlock(tableView,indexPath);

}

}

@end

在controller里面調(diào)用


#import "LeBaseTabView.h"

@property (nonatomic,strong)LeBaseTabView *trackTableView;

懶加載創(chuàng)建

- (LeBaseTabView *)trackTableView{

if (!_trackTableView) {

_trackTableView = [[LeBaseTabView alloc] initWithFrame:self.scrollView.bounds style:UITableViewStyleGrouped];

_trackTableView.backgroundColor = [UIColor clearColor];

[_trackTableView registerClass:[trackTableViewCell class] forCellReuseIdentifier:@"trackTableViewCell"];

[_trackTableView addHeaderWithTarget:self action:@selector(trackHeaderRefreshing)];

[self.scrollView addSubview:_trackTableView];

}return _trackTableView;

}

實(shí)現(xiàn)回調(diào):

-(void)createUI{

__weak typeof(self)weakSelf = self;

self.trackTableView.cellForRowAtIndexPathBlock = ^UITableViewCell *(UITableView *tableView,NSIndexPath *indexPath) {

if (indexPath.section == 0) {

trackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"trackTableViewCell" forIndexPath:indexPath];

if ([weakSelf.userTrajectoryArr count]>indexPath.row) {

BXPasterList *model = weakSelf.userTrajectoryArr[indexPath.row];

cell.model = model;

}

return cell;

}else{

trackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"trackTableViewCell" forIndexPath:indexPath];

if ([weakSelf.userTrajectoryArr count]>indexPath.row) {

BXPasterList *model = weakSelf.userFriendShareArr[indexPath.row];

cell.model = model;

}

return cell;

}

};

self.trackTableView.numberOfSectionsInTableViewBlock = ^NSInteger(UITableView *tableView) {

if ([self.userFriendShareArr count]>0) {

return 2;

}

return 1;

};

self.trackTableView.RowAtIndexPathBlock = ^NSInteger(UITableView *tableView, NSInteger section) {

if (section == 0) {

return [weakSelf.userTrajectoryArr count];

}else{

return [weakSelf.userFriendShareArr count];

}

};

self.trackTableView.heightForHeaderInSectionBlock = ^CGFloat (UITableView *tableView,NSInteger section){

if(section == 1&&[self.userFriendShareArr count]) {

return 36.f;

}else{

return 0.01;

}

};

self.trackTableView.viewForHeaderInSectionBlock = ^UIView *(UITableView *tableView,NSInteger section){

if (section==1&&[self.userFriendShareArr count]) {

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 36)];

view.backgroundColor = COLOR(250, 250, 250);

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 19, KSCREENWIDTH-30, 12)];

label.text = @"來自好友的分享";

label.font = FONT(12);

label.textColor = COLOR(100, 100, 100);

label.textAlignment = NSTextAlignmentLeft;

[view addSubview:label];

return view;

}else{

return nil;

}

};

self.trackTableView.didSelectRowAtIndexPathBlock = ^(UITableView *tableView, NSIndexPath *indexPath) {

}


后期加上下拉上拉 奉上demo ?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容