简单封装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里面实现

#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里面调用


#import "LeBaseTabView.h"

@property (nonatomic,strong)LeBaseTabView *trackTableView;

懒加载创建

- (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;

}

实现回调:

-(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  

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容