简体   繁体   English

在 UITableView 中,cell.detailTextLabel.text 不起作用……为什么?

[英]In UITableView, cell.detailTextLabel.text isn't working… why?

In tableView:cellForRowAtIndexPath: I have the following:tableView:cellForRowAtIndexPath:我有以下内容:

cell.textLabel.text = @"label";
cell.detailTextLabel.text = @"detail";

The textLabel shows as expected, but the detailTextLabel doesn't appear at all, although there is no diagnostic. textLabel按预期显示,但detailTextLabel根本没有出现,尽管没有诊断。 What I expected was that the "detail" text would appear in the cell on a second line, below the "normal" text, possibly with a smaller font size.我所期望的是“详细信息”文本将出现在第二行的单元格中,位于“正常”文本下方,可能具有较小的字体大小。

The same question is asked in another posting here and user "jbrennan" answered that the tableview cell style must be something other than UITableViewCellStylePlain .在这里的另一篇文章中提出了同样的问题,用户“jbrennan”回答说 tableview 单元格样式必须不是UITableViewCellStylePlain However, it seems there are only two possible styles, UITableViewCellStylePlain and UITableViewCellStyleGrouped .但是,似乎只有两种可能的样式, UITableViewCellStylePlainUITableViewCellStyleGrouped I get the same result with either (the detail label doesn't appear).我得到了相同的结果(没有出现详细信息标签)。

Is there another cell style that I'm not seeing in the documentation?我在文档中没有看到另一种单元格样式吗? Did UITableView change in the last update and detailTextLabel is no longer available? UITableView是否在上次更新中发生了变化并且detailTextLabel不再可用? Do I have to do something extra to make it appear?我是否必须做一些额外的事情才能让它出现? Any suggestions?有什么建议吗?

I'm using xcode 3.2.5 and building for iPhone 4.2 Simulator.我正在使用 xcode 3.2.5 并为 iPhone 4.2 Simulator 构建。

Your initialization needs to be changed to this:你的初始化需要改成这样:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
reuseIdentifier:CellIdentifier] autorelease];

I've emphasized and bolded the part you need to change.我已经强调并加粗了您需要更改的部分。

You need to set the cell type to Subtitle when you allocate it.分配时需要将单元格类型设置为副标题。

if (!cell) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier];
}

When using Xcode 4.2, set the Table View Cell style to Subtitle in Storyboard.使用 Xcode 4.2 时,将 Table View Cell 样式设置为 Storyboard 中的 Subtitle。 dequeueReusableCellWithIdentifier will return an instantiated cell. dequeueReusableCellWithIdentifier将返回一个实例化的单元格。

Set the table View Cell Style to Subtitle in Storyboard在故事板中将表格视图单元格样式设置为副标题

and write this code to configure you cell并编写此代码来配置您的单元格

if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier] autorelease];
}

在此处输入图片说明

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                           reuseIdentifier:CellIdentifier];

For Swift 4, though the technique would be the same whatever version (or Obj-C) -对于 Swift 4,尽管该技术在任何版本(或 Obj-C)中都是相同的 -

I know it's a bit late to the party, but a lot of answers are making this much more complicated than it needs to be.我知道聚会有点晚了,但是很多答案使这变得比需要的要复杂得多。

Assuming you're using a storyboard, all you need to do is set the Table View Cell Style in the storyboard to 'Subtitle', then just -假设您使用的是故事板,您需要做的就是将故事板中的表格视图单元格样式设置为“字幕”,然后只需 -

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
}

There is absolutely no need to use code to instantiate a cell, just reuse as usual.绝对不需要使用代码来实例化一个单元格,只需像往常一样重用即可。

Swift 3:斯威夫特 3:

If you're making your cell with overriding the "cellForRowAt indexPath" function:如果您通过覆盖“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
}

在此处输入图片说明

This code part from above allows you to set your detailTextLabel.. You can set whatever you want on storyboard for Table View Cell style (Custom, Basic, Right Detail, Left Detail, Subtitle), this line will override it:上面的代码部分允许您设置 detailTextLabel .. 您可以在故事板上为表格视图单元格样式(自定义、基本、右细节、左细节、副标题)设置任何你想要的,这一行将覆盖它:

style: UITableViewCellStyle.subtitle
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier: CellIdentifier];

确保设置默认文本内容,宽度是宽度足够实际显示文本的宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM