2015-02-17 72 views
0

有没有办法隐藏所有单元格的字幕,直到你选择一个单元格 - 然后它只显示你单元格的字幕?我尝试了下面的代码 - 它成功隐藏了所有字幕,但是在我选择单元格时未能显示一个:如何在选择时在UITableView中显示隐藏的字幕?

if cell.selected { 
     cell.detailTextLabel?.hidden = false 
    } else { 
     cell.detailTextLabel?.hidden = true 
    } 

感谢您的任何帮助。

编辑2 - 我结束了我的didSelectRowAtIndexPath方法这样做:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    for cell in tableView.visibleCells() { 
     cell.detailTextLabel??.hidden = true 
    } 
    var cell = tableView.cellForRowAtIndexPath(indexPath) 
    cell?.detailTextLabel?.hidden = false 

} 

非常感谢,基督徒!

回答

1

只需使用didSelectRowAtIndexPath方法并访问被触摸的单元格。然后你可以显示detailTextLabel。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell 

    cell.detailTextLabel?.hidden = false 
} 
+0

这确定看起来像我需要做的,但我不能得到它的工作。如果我没有将它们设置为隐藏在故事板中,或者它们全部隐藏并且在我选择一行时不显示 - 即使使用此覆盖,它们也都在那里。我会在上面添加更多的代码 - 也许这与其他内容有关?非常感谢,Christian – 2015-02-17 23:38:58

+0

你在哪里隐藏标签?我认为问题在于你将它隐藏在一个函数中,每次在tableview中发生一些事情时都会调用它。也许cellForRowAtIndexPath?故事板中的 – Christian 2015-02-17 23:43:52

+0

,我可以放在哪里? – 2015-02-17 23:45:19

相关问题