我在 Storyboard中创建了一个自定义 UITableView 单元格。以前我为单元格使用“字幕”样式,但我现在使用由 2 个 UILabel 组成的自定义设计。
我现在如何访问这些 UILabel?在我使用这两个自定义 UILabel 之前,我会使用:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = pollContent
cell.detailTextLabel?.text = dateString
在
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
方法
但由于单元格样式现在设置为“自定义”,我真的不能再这样做了。 我遇到的问题是我真的不知道如何使用 IBOutlets 以某种方式将这两个 UILabel 连接到我的 ViewController 类?
我还创建了一个自定义的 UITableViewCell 类并将 UILabel 连接到该类,但是如何从 ViewController 获取每个单元格的 UILabel 内容?
更新: 所以我现在用我的自定义 UITableViewCell 转换单元格,并将以下代码行添加到我的类中:
self.textLabel = titleLabel
self.detailTextLabel = subTitleLabel
其中 titleLabel 和 subTitleLabel 都是我的 UILabel。
但是我收到错误消息,指出 .textLabel 和 .detailTextLabel 都是只获取属性。
更新 2: 感谢您的所有回答/建议。正如您可能猜到的那样,我仍然是编程的新手!
我会在解决问题后回来。
更新 3:再次感谢您的帮助!现在终于可以用了。
最佳答案
您需要将单元格转换为您的自定义单元格。
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? CustomTableViewCell
cell.customLabel1.text = pollContent
cell.customLabel2.text = dateString
关于ios - UILabels 和自定义 UITableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36896720/