一种简单易用的方法来处理更新单元格相关代码,位于 tableView:cellForRowAtIndexPath:
之外。特别适用于您需要处理多个单元格类型时。
'ABTableViewCellController', '~> 1.0'
),或克隆此仓库并将 ABTableViewCellController
文件夹的内容拖放到项目中。ABTableViewCellController
协议的单元格控制器对象#import "ABTableViewCellController.h"
@interface CustomCellController : NSObject <ABTableViewCellController>
@end
@implementation CustomCellController
- (void)updateCell:(UITableViewCell *)cell withObject:(NSDictionary *)object {
cell.textLabel.text = [object objectForKey:@"name"];
cell.detailTextLabel.text = [object objectForKey:@"email"];
}
@end
#import "UITableViewCell+CellController.h"
#import "CustomCellController.h"
@implementation YourViewController {
CustomCellController *_cellController;
}
- (instancetype)init {
self = [super init];
if (self) {
_cellController = [[CustomCellController alloc] init];
}
return self;
}
#pragma mark UITableViewDataSource Methods
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"DefaultCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
cell.cellController = _cellController;
}
[cell updateCellWithObject:[_dataSource objectAtIndex:indexPath.row]];
return cell;
}
@end
ABTableViewCellController 由 Alex Bumbu 创建。
ABTableViewCellController 在 MIT 许可协议下提供。有关更多信息,请参阅 LICENSE 文件。如无归属,请联系 Alex Bumbu.