2015-02-17 88 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

相關問題