如果我想在Objective中创建一个表视图,每个单元格的定制方式不同,我将创建多个原型单元格,自定义它,并设置它自己的标识符。然后,我会添加这段代码,这样单元格就会显示出我定制它的方式。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
switch ( indexPath.row )
{
case 0:
CellIdentifier = @"fj";
break;
case 1:
CellIdentifier = @"pg";
break;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath: indexPath];
return cell;
}
我现在正在更新我的应用程序到Swift 2,并想知道如何将上面的代码更改为在Swift 2中工作。谢谢!
发布于 2015-12-21 16:21:48
给你:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellIdentifier: String
switch indexPath.row {
case 0:
cellIdentifier = "fj"
case 1:
cellIdentifier = "pg"
default:
cellIdentifier = "Cell"
}
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
return cell
}
您将注意到语法非常相似,函数调用遵循与Objective版本相同的格式。为了稍微清理它,就像@ you提到的那样,您可以将单元格标识符作为枚举进行处理,并将特定的行作为该枚举的实例存储在数组中。然后,您的开关就是存储在数组中行索引处的enum值。
https://stackoverflow.com/questions/34406536
复制相似问题