gpt4 book ai didi

ios - UITableViewCell detailTextLabel 未显示

转载 作者:IT王子 更新时间:2023-10-29 05:39:26 27 4
gpt4 key购买 nike

在 Swift 中使用 UITableView 及其 UITableViewCell。我在显示 UITableViewCelldetailTextLabel 字段时遇到一些问题。

我已经找到了这两个有用的帖子,但无法获得完全有效的解决方案: 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
}

当我尝试使用 UITableViewCelldetailTextLabel 字段时,它没有显示。在网上搜索时,我知道我必须为单元格使用正确的样式 (UITableViewCellStyle),但我看不到如何将该更改集成到我的代码中。我看到的示例基于 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。然后,您可以生成具有指定样式的自己的单元格。

关于ios - UITableViewCell detailTextLabel 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34101102/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com