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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容