一个使用基于块的代理代替协议的 iOS 表视图。试试吧,我们敢
MTBlockTableView
是一个实验性的 `UITableView` 的替换,它使用块来执行所有协议方法
- (void)viewDidLoad
{
[super viewDidLoad];
[_tableView setNumberOfRowsInSectionBlock:^NSInteger(UITableView *tableView, NSInteger section) {
return 10;
}];
[_tableView setCellForRowAtIndexPathBlock:^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text = [@(indexPath.row) stringValue];
return cell;
}];
}
该库的动力是证明概念,即通过协议的代理可以通过基于块的代理来实现,在许多情况下将更加表达和方便。请参见 此博客文章 了解更多详细示例。