美文网首页
简单封装tableview把代理换成Block

简单封装tableview把代理换成Block

作者: writeSpace | 来源:发表于2017-11-02 13:52 被阅读160次

直接上代码了

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  

相关文章

  • 简单封装tableview把代理换成Block

    直接上代码了 LeBaseTabView.h里面 #import@interface LeBaseTabView ...

  • view的封装

    封装view较为简单,封装tableview比较麻烦,封装tableview的方法后面会有。 view的封装 如果...

  • runloop优化tableView的实现

    runloop如何优化tableView 回答:把任务以block块的方式封装起来,存放到任务数组中,若任务数组中...

  • 个性化 UIAlertController

    系统的 UIAlertController 封装的很漂亮,用block代替之前 UIAlertView 的代理,用...

  • iOS 单例block简单Demo

    简单block封装afnetworking MMAFNetWorking.h MMAFNetWorking.m 然...

  • Block 初探

    在介绍Block之前通过一个简单的应用场景认识下Block 场景描述如下:TableView上面有多个Custom...

  • OC-简单粗暴理解Block的本质

    block简单粗暴的理解 OC的block底层就是个OC对象,包含isa指针,封装了函数的调用. OC的block...

  • 【自学java】 代理模式

    简单的几句话讲能不能讲明白代理模式…… 一、简介 什么是代理模式答: 代理模式是一种封装,将被代理对象封装到代理对...

  • iOS 实现TextField的picker效果

    只是简单的实现TextField的picker效果可扩展:1 . pickerView换成TableView、Da...

  • 自定义弹出选择列表

    基于tableview封装的弹出选择列表,效果图: 使用起来非常方便。 首先定义自己的代理方法 pragma ma...

网友评论

      本文标题:简单封装tableview把代理换成Block

      本文链接:https://www.haomeiwen.com/subject/rwvopxtx.html