2015-01-07 41 views
1

我希望每个单元都有detailTextLabel显示。iOS UITableViewCell detailTextLabel不显示

细胞被实例化(在cellForRowAtIndexPath:)有:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

而且我tryed设置样式类型有:

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

(我的Xcode给ARC与自动发布警告,所以我也尝试过它省略。虽然相同的结果)

我有点困惑。显然,如果没有cell == nil,代码的第一部分是徒劳的,但对于它来说,单元格从来没有detailTextLabel显示。 (是的,cell.detailTextLabel.text设置正确)

我该怎么办呢?

更新:因为我正在使用故事板,我能够通过将单元格样式设置为'副标题'来实现所需的结果。然而,如何编程的这个问题仍然存在

+0

如果您启用了ARC而不是'autorelease',则不需要。 – Kampai

+0

在这个地方使用自定义单元格 –

+1

你在使用故事板吗?如果是的话,看看这个线程 http://stackoverflow.com/questions/15424453/why-is-my-uitableviewcell-not-showing-detailtextlabel-in-any-style – riyaz

回答

0

更改为下面的代码应该在编程时执行此操作。 (感谢确实到mbm29414)

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
            reuseIdentifier:@"Cell"]; 
} 
0

创建表格单元格的现代途登记单元格,然后用食指路径出队了。问题是,如果您注册UITableViewCell,您将永远得到default类型的单元格。

解决方案是子类UITableViewCell并在其中设置样式。例如:

class SubtitleTableViewCell: UITableViewCell { 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) 
    } 

    required init?(coder aDecoder: NSCoder) { 
    fatalError() 
    } 
} 

现在,在注册时使用您的子类。

let table = UITableView(frame: .zero, style: .plain) 
table.register(DebugTableViewCell.self, forCellReuseIdentifier: identifier) 
相关问题