[英]In UITableView, cell.detailTextLabel.text isn't working… why?
在tableView:cellForRowAtIndexPath:
我有以下内容:
cell.textLabel.text = @"label";
cell.detailTextLabel.text = @"detail";
textLabel
按预期显示,但detailTextLabel
根本没有出现,尽管没有诊断。 我所期望的是“详细信息”文本将出现在第二行的单元格中,位于“正常”文本下方,可能具有较小的字体大小。
在这里的另一篇文章中提出了同样的问题,用户“jbrennan”回答说 tableview 单元格样式必须不是UITableViewCellStylePlain
。 但是,似乎只有两种可能的样式, UITableViewCellStylePlain
和UITableViewCellStyleGrouped
。 我得到了相同的结果(没有出现详细信息标签)。
我在文档中没有看到另一种单元格样式吗? UITableView
是否在上次更新中发生了变化并且detailTextLabel
不再可用? 我是否必须做一些额外的事情才能让它出现? 有什么建议吗?
我正在使用 xcode 3.2.5 并为 iPhone 4.2 Simulator 构建。
你的初始化需要改成这样:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
我已经强调并加粗了您需要更改的部分。
分配时需要将单元格类型设置为副标题。
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier];
}
使用 Xcode 4.2 时,将 Table View Cell 样式设置为 Storyboard 中的 Subtitle。 dequeueReusableCellWithIdentifier
将返回一个实例化的单元格。
在故事板中将表格视图单元格样式设置为副标题
并编写此代码来配置您的单元格
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier] autorelease];
}
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
对于 Swift 4,尽管该技术在任何版本(或 Obj-C)中都是相同的 -
我知道聚会有点晚了,但是很多答案使这变得比需要的要复杂得多。
假设您使用的是故事板,您需要做的就是将故事板中的表格视图单元格样式设置为“字幕”,然后只需 -
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myIdentifier", for: indexPath)
cell.textLabel?.text = "Title"
cell.detailTextLabel?.text = "Subtitle..."
return cell
}
绝对不需要使用代码来实例化一个单元格,只需像往常一样重用即可。
斯威夫特 3:
如果您通过覆盖“cellForRowAt indexPath”函数来制作单元格:
let tableContent = ["1", "2", "3", "4", "5"]
let tableDetailContent = ["a", "b", "c", "d", ""]
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "yourCellIdentifier")
cell.textLabel?.text = tableContent[indexPath.row]
cell.detailTextLabel?.text = tableDetailContent[indexPath.row]
return cell
}
上面的代码部分允许您设置 detailTextLabel .. 您可以在故事板上为表格视图单元格样式(自定义、基本、右细节、左细节、副标题)设置任何你想要的,这一行将覆盖它:
style: UITableViewCellStyle.subtitle
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier: CellIdentifier];
确保设置默认文本内容,宽度是宽度足够实际显示文本的宽度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.