TableView封装心得 去掉TableView中委托里麻烦的重复代码。

手写继承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);///<配置CellBlock外部只要写CellSet方法即可,不用实例化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];


}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#import "LHDBaseTableViewCell.h" @interface LHDBaseTableView : UITableView <UITableViewDataSource,UITableViewDelegate> @property (nonatomic, assign) CGFloat cellHeight; @property (nonatomic, assign) BOOL fixed; //是否固定高度 @property (nonatomic, copy) NSInteger(^tableViewNumberOfRowInSection)(UITableView *,NSInteger); @property (nonatomic, copy) UITableViewCell *(^tableViewCellForRowAtIndexPath)(UITableView *, NSIndexPath *); @property (nonatomic, copy) NSInteger(^numberOfSectionInTabelView)(UITableView *); @property (nonatomic, copy) CGFloat(^tableViewHeightForRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) BOOL(^tableViewCanEditRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) void(^tableViewCommitEditingStyleforRowAtIndexPath)(UITableView *,UITableViewCellEditingStyle,NSIndexPath *); @property (nonatomic, copy) UITableViewCellEditingStyle (^tableViewEditingStyleForRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) void(^tableViewDidSelectRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) void(^tableViewDidDeselectRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) UIView *(^tableViewViewForHeaderInSection)(UITableView *,NSInteger); @property (nonatomic, copy) CGFloat(^tableViewHeightForHeaderInSection)(NSInteger); @property (nonatomic, strong) LHDBaseTableViewCell *myTableViewCell; @property (nonatomic, strong) NSArray *dataArray; - (void)resetDelegate;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值