【发布时间】:2014-07-26 13:41:01
【问题描述】:
我想要一个带有subtitle 样式单元格的UITableView,并使用dequeueReusableCellWithIdentifier。
我原来的 Objective-C 代码是:
static NSString *reuseIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
在 SO 上搜索了几个 UITableView 问题后,我想像这样用 Swift 编写它:
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
但这并不能让我说我想要subtitle 风格。所以我尝试了这个:
var cell :UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
这给了我一个subtitle 单元格,但它没有让我dequeueReusableCellWithIdentifier。
我研究了更多内容并查看了 this video tutorial,但他创建了一个单独的 subclass 和 UITableViewCell,我认为这是不必要的,因为我之前在 Obj-C 中完成了同样的效果。
有什么想法吗?谢谢。
【问题讨论】:
-
你能澄清一下“它不允许我
dequeueReusableCellWithIdentifier”是什么意思吗?我不清楚。 -
@68cherries 抱歉,我不是指“功能”,不知道该怎么称呼它。方法?基本上,我希望我的单元格出列,正如我在 SO 上所读到的那样,如果您使用 dequeueReusableCellWithIdentifier,它会重用单元格,因此效率更高。
-
为什么不用和以前一样的代码呢?如果是
nil则将一个单元格出列使用 swift constructor 初始化一个新单元格。 -
由于单元格不再是
nil,这里的答案都是过时的除了Meilbn's answer
标签: ios uitableview swift