【问题标题】:Control UITableViewCell's style控制 UITableViewCell 的样式
【发布时间】:2016-02-28 21:55:27
【问题描述】:

我想按原样使用UITableViewCell,但将其样式设置为Subtitle。我的理解是样式只能在初始化器中设置。问题是初始化器被隐式调用了,我没办法指定样式。

在我的UITableViewControllerviewDidLoad() 中注册单元并重复使用 ID

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "defaultCell")

然后在我的cellForRowAtIndexPath

let cell = tableView.dequeueReusableCellWithIdentifier("defaultCell", forIndexPath: indexPath)

cell 总是非零。另一种方法是绕过单元重用并始终创建一个新实例

let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "defaultCell")

有没有办法指定样式并获得单元格重用?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    您无需绕过单元重复使用即可获得所需的内容。用老式的方式做。

    不要打电话给registerClass:forCellReuseIdentifer:

    使用dequeueReusableCellWithIdentifer: 而不是dequeueReusableCellWithIdentifer:forIndexPath:

    当对dequeueReusableCellWithIdentifer: 的调用返回nil 时,您可以显式创建具有所需样式的单元格。

    【讨论】:

    • 即使调用tableView.dequeueReusableCellWithIdentifier("defaultCell"),它总是返回非零
    • 你必须仍然调用registerClass:forCellReuseIdentifer:或其他类似的方法。
    • 啊,我明白了。我打电话给registerClass,因为没有它我得到unable to dequeue a cell with identifier defaultCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard。但是,调用dequeueReusableCellWithIdentifier(没有forIndexPath)确实可以在没有registerClass 的情况下工作。谢谢!
    • 我真正想要构建的是这个stackoverflow.com/questions/35686628,我很想听听你的意见
    • 这个问题已经回答了。我会看看你的另一个问题。
    【解决方案2】:

    您可以在 Storyboard 中更改样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多