2017-02-18 74 views
1

我的tableview細胞字幕沒有顯示時,我用這個:泰伯維細胞字幕沒有顯示或應用程序退出

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    var cell:UITableViewCell? 

    if tableView.tag == 1 { 

     guard let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell") else { 
      return UITableViewCell(style: .subtitle, reuseIdentifier: "latestCell") 
     } 



     latestCell.textLabel?.text = latest[indexPath.row] 

     latestCell.detailTextLabel?.text = latestSub[indexPath.row] 

     latestCell.accessoryType = .disclosureIndicator 

     return latestCell 


    } 
} 

不過,如果我用這個:

else if tableView.tag == 2 { 

     let olderCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "olderCell") 



     olderCell.textLabel?.text = older[indexPath.row] 

     olderCell.detailTextLabel?.text = olderSub[indexPath.row] 

     olderCell.accessoryType = .disclosureIndicator 

     return olderCell 
    } 

    else { 
     return cell! 
    } 
} 

字幕加載完美,但在關閉應用程序並重新加載視圖後,應用程序會自動進行自動處理,而不會提供崩潰日誌或帶我進入調試選項卡。

我知道數據來自的數組很好,我認爲我已經在故事板中設置了一切。關於這個問題已經發布了很多類似的問題,但是他們似乎都忘記了將cellStyle設置爲.subtitle。預先感謝我獲得的任何幫助!

順便說一句。我的常規單元格標題正如我想要的那樣工作。那裏沒問題。

回答

1

在第一部分中,在設置單元格的文本和詳細信息文本之前,警戒語句會返回。如果將其更改爲:

if let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell") { 
    cell = latestCell    
} else { 
    cell = UITableViewCell(style: .subtitle, reuseIdentifier: "latestCell") 
}    
cell.textLabel?.text = latest[indexPath.row] 

cell.detailTextLabel?.text = latestSub[indexPath.row] 

cell.accessoryType = .disclosureIndicator 

return cell 

標籤現在將被設置。

而且你的崩潰是由這個原因引起:

return cell! 

如果細胞==零,然後將細胞!將嘗試解開它。真的,你應該做的是調用超級的實現:

return super.tableView(tableView, cellForRowAt: indexPath) 

祝你好運。

+0

我無法返回super.tableview,Xcode不會允許它。除此之外,我改變了代碼,但它仍然沒有顯示字幕,但它也沒有崩潰。感謝您的回覆! – Tuomax

+0

你有什麼補充或其他解決方案嗎? – Tuomax

+0

抱歉沒有早點回來。它看起來應該可以工作 - 也許在'cell.detailTextLabel?.text = latestSub [indexPath.row]'之後設置一個斷點,然後在lldb中輸入'p cell.detailTextLabel?.text'來查看它是否設置正確。當你在它的時候輸入'p latestSub [indexPath.row]'來查看你的數組是否正確填充。 – joeybladb