【发布时间】:2015-12-05 03:59:55
【问题描述】:
在 Swift 中使用 UITableView 及其 UITableViewCell(s)。我在显示 UITableViewCell 的 detailTextLabel 字段时遇到了一些问题。
我已经找到了这两个有用的帖子,但无法获得完全有效的解决方案: swift detailTextLabel not showing up How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?
这是我正在使用的代码,与我的问题相关:
override func viewDidLoad() {
super.viewDidLoad()
………….
theTableView = UITableView(frame: tmpFrame)
………….
theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
theTableView.dataSource = self
theTableView.delegate = self
self.view.addSubview(theTableView)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor()
cell.textLabel?.text = "THE-TEXT-LABEL"
cell.detailTextLabel?.text = "KIJILO" // This does not show up.
return cell
}
当我尝试使用UITableViewCell 的detailTextLabel 字段时,它没有出现。在网上搜索,我知道我必须为单元格使用正确的样式(UITableViewCellStyle),但我不知道如何将该更改集成到我的代码中。我看到的示例是基于子类化UITableViewCell,我想我不需要子类化UITableViewCell 只是为了使用detailTextLabel 字段。如果我错了,请告诉我。
我也尝试了上面提到的帖子中的一些东西,但没有任何效果。
【问题讨论】:
-
您需要您发布的第二个链接中的代码,但前提是您删除了
theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")行的使用。
标签: ios swift uitableview