2017-02-19 88 views
0

我的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。預先感謝我獲得的任何幫助!

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

編輯:

我認爲問題是,我可以創造也沒有問題的默認風格細胞。但是當我嘗試將樣式設置爲.subtitle時,它會在第一次正確加載,但第二次打開時會崩潰。有沒有辦法將這兩個聲明一起使用,以避免互相排斥;?

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

和:

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

A link to a view of how my cells are set up in the storyboard:

+0

在你守護讓的東西,你甚至沒有設置其屬性返回的單元格。第二個代碼不重用單元格(不推薦)。 – Larme

+0

朋友我可以問你嗎?你使用兩個tableview嗎? –

+0

是的,我正在使用兩個。 – Tuomax

回答

0

做這樣的:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("latestCell") as UITableViewCell 

    cell.textLabel?.text = latest[indexPath.row] 
    cell.detailTextLabel?.text = latestSub[indexPath.row] 
    cell.accessoryType = .disclosureIndicator 

    return cell 

} 

將此標記解決方案/給予好評,如果這個解決您的問題。

+0

恐怕這不會將cellStyle設置爲副標題,所以它沒有區別。 – Tuomax

0

最簡單的辦法:

設計細胞在Interface Builder直接在表視圖(S),設置樣式和附件查看到您所需的值,並添加標識。

然後代碼可以減少到。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    switch tableView.tag { 
    case 1: 
    let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell" for: indexPath) 
     latestCell.textLabel!.text = latest[indexPath.row] 
     latestCell.detailTextLabel!.text = latestSub[indexPath.row] 
     return latestCell 

    case 2: 
     let olderCell = tableView.dequeueReusableCell(withIdentifier: "olderCell" for: indexPath) 
     olderCell.textLabel!.text = older[indexPath.row] 
     olderCell.detailTextLabel!.text = olderSub[indexPath.row] 
     return olderCell 

    default: 
     fatalError("That should never happen") 
    } 

} 

由於細胞被預定義爲副標題的風格,既textLabeldetailTextLabel保證存在,可以安全地展開。

然而,細胞之間有什麼顯着差異。從給定的代碼中,您實際上可以使用一個單元格(標識符cell)。這可以使代碼更短:

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell" for: indexPath) 

    if tableView.tag == 1 { 
     cell.textLabel!.text = latest[indexPath.row] 
     cell.detailTextLabel!.text = latestSub[indexPath.row] 
    } else { 
     cell.textLabel!.text = older[indexPath.row] 
     cell.detailTextLabel!.text = olderSub[indexPath.row] 
    } 
    return cell 
} 
+0

這不會將樣式設置爲副標題,所以在那裏沒有運氣,而且我已經在故事板中正確設置了單元格。但是,謝謝你的嘗試! – Tuomax

+0

如果樣式在Interface Builder中設置,它必須出現在重用的單元格中。 – vadian

+0

你是什麼意思?對不起,我不太明白。如果您的意思是在故事板中單元格中設置的樣式,那麼是的,它顯示在那裏。 – Tuomax

0
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") //replace "Cell" with your identifier 
    cell.textLabel = yourTitleArray[indexPath.row] 
    cell.detailTextLabel = yourSubtitleArray[indexPath.row] 
    return cell! 
} 
+0

對不起,沒有工作,應用程序崩潰。 – Tuomax

+0

你會得到什麼錯誤? –