2011-01-29 101 views
41

tableView:cellForRowAtIndexPath:我有以下几点:在UITableView中,cell.detailTextLabel.text不工作...为什么?

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

textLabel显示为预期,但detailTextLabel不会出现在所有的,虽然没有诊断。我期望的是,“细节”文本将出现在第二行的单元格中,低于“正常”文本,可能具有较小的字体大小。

在另一个帖子中询问同样的问题,用户“jbrennan”回答说tableview单元格样式必须是除UITableViewCellStylePlain以外的其他东西。但是,似乎只有两种可能的款式,UITableViewCellStylePlainUITableViewCellStyleGrouped。我得到相同的结果(细节标签不出现)。

在文档中没有看到另一种单元格样式吗? UITableView在上次更新中有变化,detailTextLabel不再可用?我是否需要额外做些事情才能让它出现?有什么建议么?

我正在使用xcode 3.2.5并为iPhone 4.2建模模拟器。

回答

111

你的初始化需要改变这样:

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

我一直强调并加粗你需要改变的部分。

+0

对!谢谢。我曾尝试将样式更改为Subtitle,但我更改了UITableViewStyle而不是UITableViewCellStyle,但未编译,因此我认为Subtitle样式不再可用。 – RobertL 2011-01-29 00:18:15

+1

哈哈!编码时我们都犯了愚蠢的错误。我认为,如果苹果禁用了字幕风格,就会出现骚乱。 – 2011-01-29 00:19:18

14

您需要在分配单元格时将单元格类型设置为子标题。

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

当使用Xcode 4.2时,将Table View Cell style设置为Storyboard中的Subtitle。 dequeueReusableCellWithIdentifier将返回一个实例化的单元格。

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

设置表格视图单元格样式在故事板

以字幕和编写代码来配置你细胞

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

enter image description here

0

斯威夫特3:

如果你是米

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 
} 

enter image description here

这部分代码从上面可以让你设置你的detailTextLabel ..你可以设置任何你想要的故事板的表视图单元格样式:与重写“cellForRowAt indexPath”功能亚庆你的(自定义,基本,右侧细节,左侧细节,小标题),此行将覆盖它:

style: UITableViewCellStyle.subtitle