iOS UITableView 分离UITableViewDataSource 和 UITableViewDelegate

前言

最近的iOS开发中遇到了一个问题,就是一个基本相同的TableView需要在多个控制器中使用,如果每一个控制器都把大部分相同的代码都复制了一遍,这样显然不符合开发的理念,我就想着把UITableViewDataSource和UITableViewDelegate的代理方法提取出来,放到一个基类里面,所有需要这个UITableView的共有的Cell,都放入到这个基类中,每个页面中不同的部分放入到这个基类的子类中,去自定义.

具体实现

创建一个继承自NSObject的基类,并且使它服从UITableViewDataSource和UITableViewDelegate协议.

@interface LRBaseMainTableViewDataSource : NSObject <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *mainTableView;
- (instancetype)initWithTableView:(UITableView *)mainTableView withNaVC:(BaseNavigationController *)naVC;
@property (nonatomic, strong) NSMutableArray *cateArray;
@property (nonatomic, strong) BaseNavigationController *naVC;
@end

在.m文件中,实现它服从协议的代理方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LRCateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LRCateTableViewCell" forIndexPath:indexPath];
            return cell;
}
#pragma mark - tableViewDelgate &datasoruce
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     return 196;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cateArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 10.0;
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

上面的这些方法里面的实现都是常规的UITableView的一些实现,是UITableView里面我们共有的部分,下面的这才是我们实现代码的重用和个性化定制的方法,下面我们再建一个继承成自LRBaseMainTableViewDataSource,实现如下:

#import "LRBaseMainTableViewDataSource.h"
#import "LRMakeOrderSuccessCell.h"

@interface LRMakeOrderDataSource : LRBaseMainTableViewDataSource <LRMakeOrderSuccessCellDelegate>
- (instancetype)initWithTableView:(UITableView *)mainTableView withNaVC:(BaseNavigationController *)naVC;
@end

在其实现文件中我们就可以重写他的那些协议的代理方法啦,

#import "LRMakeOrderDataSource.h"
#import "LRMyOrderViewController.h"

@implementation LRMakeOrderDataSource
- (instancetype)initWithTableView:(UITableView *)mainTableView withNaVC:(BaseNavigationController *)naVC{
    self = [super initWithTableView:mainTableView withNaVC:naVC];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"LRMakeOrderSuccessCell" bundle:nil] forCellReuseIdentifier:@"LRMakeOrderSuccessCell"];
    return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        LRMakeOrderSuccessCell *lrMakeOrderSuccessCell = [tableView dequeueReusableCellWithIdentifier:@"LRMakeOrderSuccessCell" forIndexPath:indexPath];
            lrMakeOrderSuccessCell.delegate = self;
        lrMakeOrderSuccessCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return lrMakeOrderSuccessCell;
    }else{
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == 0)
    {
        return CGFLOAT_MIN;
    }else{
        return [super tableView:tableView heightForFooterInSection:section];
    }
}

在这些代理方法的内容中,需要我们重新定制的cell我们来进行单独的处理,如果不需要我们单独处理的我们只需要调用其父类的就可以啦例如:


94CFE12A-C292-45D8-B647-40710196649C.png

上面的部分就是每一个控制器里面我们根据不同的情况来定义的,共同部分我们不需要写只是调用基类的就行.

结尾

不经常写博客,希望多多支持啊.

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

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,209评论 30 471
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,088评论 3 38
  • 转:http://www.cocoachina.com/programmer/20151019/13746.htm...
    Style_伟阅读 1,331评论 0 3
  • 之前看了很多面试题,感觉要不是不够就是过于冗余,于是我将网上的一些面试题进行了删减和重排,现在分享给大家。(题...
    Job_Yang阅读 12,109评论 12 143
  • 前言 matplotlib, 官方提供的饼图Demo,功能比较比较简单,在实际应用过程中,往往会有许多个性化的绘制...
    leenard阅读 5,680评论 0 11