在Swift中使用aUITableView
及其UITableViewCell
(s)。我在显示的detailTextLabel
字段时遇到一些问题UITableViewCell
。
我已经找到了这两个有用的帖子,但是找不到完整的解决方案:swift detailTextLabel不显示 如何在Swift中设置UITableViewCellStyleSubtitle和dequeueReusableCell?
这是我使用的与我的问题有关的代码:
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
}
当我尝试使用的detailTextLabel
字段时UITableViewCell
,它没有显示。在网上搜索时,我了解到必须UITableViewCellStyle
为单元格使用适当的style(),但是我看不到如何将更改集成到代码中。我看到的示例都是基于子类的UITableViewCell
,我想我不需要UITableViewCell
仅子类即可使用该detailTextLabel
字段。如果我错了,请告诉我。
我也从上面提到的帖子中尝试了一些方法,但是没有任何所需的方法。
您已经注册了UITableViewCell
theTableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "reuseIdentifier")
这意味着当您致电
tableView.dequeueReusableCellWithIdentifier("reuseIdentifier",
forIndexPath: indexPath)
将使用UITableViewCellStyleDefault样式自动为您创建一个样式。
因为您想要自定义样式,所以需要做一些事情:
消除
theTableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "reuseIdentifier")
代替
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier",
forIndexPath: indexPath)
与以下
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")
if cell == nil {
cell = UITableViewCell(style: .Value1, reuseIdentifier: "reuseIdentifier")
}
dequeueReusableCellWithIdentifier
如果无法使单元出队,则这里发生的情况将返回nil。然后,您可以生成具有指定样式的自己的单元格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句