iOS-整洁的TableView

Apple 提供了 UITableViewController作为 TableView 专属的 ViewController 类。Table view controllers 实现了一些非常有用的特性,来帮你避免一遍又一遍地写那些死板的代码!但是话又说回来,table view controller 只限于管理一个全屏展示的 table view。大多数情况下,这就是你想要的,但如果不是,还有其他方法来解决这个问题。

1.使用 Child View Controllers

和完全抛弃 TableViewController 不同,你还可以将它作为ChildView Controller 添加到其他ViewController 中。ParentViewController 在管理其他的你需要的新加的界面元素的同时TableViewController 还可以继续管理它的TableView。

ChildTableViewController *child = [[ ChildTableViewController alloc] init];
child.delegate = self;
[self child];

child.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, 200);
[self.view child.view];
[child didMoveToParentViewController:self];

如果你使用这个解决方案,你就必须在 ChildView Controller 和 ParentViewController 之间建立消息传递的渠道。比如,如果用户选择了一个TableView 中的 cell,ParentViewController 需要知道这个事件来推入其他 ViewController。根据使用习惯,通常最清晰的方式是为这个TableViewController 定义一个 protocol,然后到 ParentViewController 中去实现。

2.分离TableView

把 UITableViewDataSource 的代码提取出来放到一个单独的类中。使用一个 block 来设置 cell,也可以用 delegate 来做这件事。

创建一个 ArrayDataSource 类的实例作为 table view 的 data source。

.h文件:

typedef void (^TableViewCellConfigureBlock)(id cell,id model);

@interface ArrayDataSource : NSObject <UITableViewDataSource>

- (id)initWithModels:(NSArray *)models
      cellIdentifier:(NSString *)identifier
      ConfigureBolck:(TableViewCellConfigureBlock)configureBlock;

- (id)modelAtIndexPath:(NSIndexPath *)indexPath;

@end

.m文件:

@interface ArrayDataSource ()
@property (nonatomic, strong) NSArray *models;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
@end

@implementation ArrayDataSource

- (id)initWithModels:(NSArray *)models cellIdentifier:(NSString *)identifier ConfigureBolck:(TableViewCellConfigureBlock)configureBlock {
    self = [super init];
    if (self) {
        self.models = models;
        self.cellIdentifier = identifier;
        self.configureCellBlock = configureBlock;
    }
    return self;
}

- (id)modelAtIndexPath:(NSIndexPath *)indexPath {
    return self.models[indexPath.row];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    id model = [self modelAtIndexPath:indexPath];
    self.configureCellBlock(cell, model);
    return cell;
}


使用方法:

(1)声明属性

@property (nonatomic, strong) ArrayDataSource *arrayDataSource;

(2)设置ArrayDataSource

TableViewCellConfigureBlock configureBlock = ^(TableViewCell *cell, Model *model) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell fillWithModel:model];
};

self.arrayDataSource = [[ArrayDataSource alloc] initWithModels:self.dataSource cellIdentifier:@"cell" ConfigureBolck:configureBlock];
    
self.tableview.dataSource = self.arrayDataSource;

每次你想把这个数组显示到一个 table view 中时,你都可以复用这些代码。你也可以实现一些额外的方法,比如 tableView:commitEditingStyle:forRowAtIndexPath:。

Demo地址:https://github.com/olivierzh/CleanTableView.git

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

推荐阅读更多精彩内容