前言
最近的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
上面的部分就是每一个控制器里面我们根据不同的情况来定义的,共同部分我们不需要写只是调用基类的就行.
结尾
不经常写博客,希望多多支持啊.
网友评论