【发布时间】:2015-08-11 03:59:55
【问题描述】:
保持 ViewController 精简并使用 MVMC 帮助我更轻松地进行维护。 Obj.io 在他们的site 上有一个非常好的教程。不幸的是,本教程仅在 Objective-C 中。
我正在尝试切换到 Swift 并且非常迅速地移动,直到我到达用于配置单元的块。
在教程中他们创建了一个区块typedef:
typedef void (^TableViewCellConfigureBlock)(id cell, id item);
在创建单元格时返回cellForRowAtIndexPath 中的单元格。
以下是部分代码和整个项目:Project
void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {
cell.label.text = photo.name;
};
photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
cellIdentifier:PhotoCellIdentifier
configureCellBlock:configureCell];
self.tableView.dataSource = photosArrayDataSource;
cellForRowAtIndexPath:
- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath {
id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath];
configureCellBlock(cell,item);
return cell;
}
问题:如何在 Swift 1.2+ 中完成同样的任务?我应该使用closure、protocol delegate 等吗?更重要的是,我如何在cellForRowAtIndexPath 中调用它?
我是 Swift 新手,非常感谢您提供解决方案代码。谢谢。
【问题讨论】:
标签: objective-c swift closures objective-c-blocks