UITableView 这个控件,想必做iOS开发的没有一个不知道是做什么的,基本上每天都在和它进行打交道,不断的优化,优化,再优化.关于优化的在这里先不说,度娘上有不计其数的方法的Demo,这里主要讲一下对UITableView的数据源进行抽离的思路和简单地实现(这不也是优化么?);
#import "ViewController.h"
static NSString * const kCellIdentigier = @"CellID";
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
#pragma mark - Live Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self defaultConfig];
}
#pragma mark - Private Method
- (void)defaultConfig {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)
style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 100;
[self.view addSubview:_tableView];
}
#pragma mark - Public Method
#pragma mark - Delegate
//UITableViewDataSourceDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentigier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentigier];
}
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
return cell;
}
//UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Getter And Setter Method
#pragma mark - Dealloc
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行后的结果是
这个就不用说了,没毛病.
接下来我们就要对UITableView的数据源进行抽离;
第一步:我们应该新创建一个类来进行UITableViewDataSources的管理,这里我就创建了这么一个类 HLTableViewDataSource 用它来进行管理;
第二步:就是我们抽离数据都抽离那些,这里我就简单的帮大家屡一下思路,我就简单的抽离一些常用的代理,如下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
第三步:有了想抽了的代理,那就应该进行写实例化方法了,把这些代理都让外部进行传参,代码如下:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface HLTableViewDataSource : NSObject<UITableViewDataSource>
//实例化表格视图
- (instancetype)initWithItems:(nullable NSArray *)items
cellIdentifier:(nullable NSString *)cellIdentifier
tableViewCellStytle:(UITableViewCellStyle)tableViewCellStytle
configCellBlock:(void(^)(UITableViewCell *cell ,id item,NSIndexPath *indexPath))configCellBlock;
@end
NS_ASSUME_NONNULL_END
因为 item 因素不确定,因此这里给了一个id类型;
第四步:有了声明那就来让我们实现它代码如下:
#import "HLTableViewDataSource.h"
#define HL_SAFE_BLOCK(BlockName, ...) ({ !BlockName ? nil : BlockName(__VA_ARGS__); })
typedef void (^CellBlock)(UITableViewCell *cell, id item, NSIndexPath*indexPath);
@interface HLTableViewDataSource ()
@property (nonatomic, copy) NSArray *dataSource;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) CellBlock cellBlcok;
@property (nonatomic, assign) UITableViewCellStyle tableViewCellStytle;
@end
@implementation HLTableViewDataSource
- (instancetype)initWithItems:(NSArray *)items
cellIdentifier:(NSString *)cellIdentifier
tableViewCellStytle:(UITableViewCellStyle)tableViewCellStytle
configCellBlock:(void (^)(UITableViewCell * _Nonnull, id _Nonnull, NSIndexPath * _Nonnull))configCellBlock {
if (self = [super init]) {
_dataSource = [NSArray array];
//进行赋值
_dataSource = items;
_cellIdentifier = cellIdentifier;
_tableViewCellStytle = tableViewCellStytle;
_cellBlcok = configCellBlock;
}
return self;
}
#pragma mark - UITableViewDataSourceDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:_tableViewCellStytle
reuseIdentifier:_cellIdentifier];
}
//回调
HL_SAFE_BLOCK(_cellBlcok,cell,_dataSource[indexPath.row],indexPath);
return cell;
}
@end
到此我们抽离UITableView的数据源的类就已经创建好了,那让我们来替换一下;
第五步:我们把关于数据源的代码都有我们创建的HLTableViewDataSource 来管理 整理如下代码:
#import "ViewController.h"
#import "HLTableViewDataSource.h"
static NSString * const kCellIdentigier = @"CellID";
@interface ViewController ()<UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HLTableViewDataSource *tableViewDataSources;
@end
@implementation ViewController
#pragma mark - Live Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self defaultConfig];
}
#pragma mark - Private Method
- (void)defaultConfig {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)
style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self.tableViewDataSources;
_tableView.rowHeight = 100;
[self.view addSubview:_tableView];
}
#pragma mark - Public Method
#pragma mark - Delegate
//UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Getter And Setter Method
- (HLTableViewDataSource *)tableViewDataSources {
if (_tableViewDataSources == nil) {
self.tableViewDataSources = [[HLTableViewDataSource alloc] initWithItems:@[@1,@2,@3,@4,@25,@6]
cellIdentifier:kCellIdentigier
tableViewCellStytle:UITableViewCellStyleDefault
configCellBlock:^(UITableViewCell * _Nonnull cell, id _Nonnull item, NSIndexPath * _Nonnull indexPath) {
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.text = [NSString stringWithFormat:@"%@",item];
}];
}
return _tableViewDataSources;
}
#pragma mark - Dealloc
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
OK!整理好了,让我们来Run一下看看;
Beautiful 成功!截个图看看
到此整理结束!
仅供参考,如有不足,多谢指正!