2011-01-29 100 views
41

tableView:cellForRowAtIndexPath:我有以下幾點:在UITableView中,cell.detailTextLabel.text不工作...爲什麼?

cell.textLabel.text = @"label"; 
cell.detailTextLabel.text = @"detail"; 

textLabel顯示爲預期,但detailTextLabel不會出現在所有的,雖然沒有診斷。我期望的是,「細節」文本將出現在第二行的單元格中,低於「正常」文本,可能具有較小的字體大小。

在另一個帖子中詢問同樣的問題,用戶「jbrennan」回答說tableview單元格樣式必須是除UITableViewCellStylePlain以外的其他東西。但是,似乎只有兩種可能的款式,UITableViewCellStylePlainUITableViewCellStyleGrouped。我得到相同的結果(細節標籤不出現)。

在文檔中沒有看到另一種單元格樣式嗎? UITableView在上次更新中有變化,detailTextLabel不再可用?我是否需要額外做些事情才能讓它出現?有什麼建議麼?

我正在使用xcode 3.2.5併爲iPhone 4.2建模模擬器。

回答

111

你的初始化需要改變這樣:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];

我一直強調並加粗你需要改變的部分。

+0

對!謝謝。我曾嘗試將樣式更改爲Subtitle,但我更改了UITableViewStyle而不是UITableViewCellStyle,但未編譯,因此我認爲Subtitle樣式不再可用。 – RobertL 2011-01-29 00:18:15

+1

哈哈!編碼時我們都犯了愚蠢的錯誤。我認爲,如果蘋果禁用了字幕風格,就會出現騷亂。 – 2011-01-29 00:19:18

14

您需要在分配單元格時將單元格類型設置爲子標題。

if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier]; 
} 
12

當使用Xcode 4.2時,將Table View Cell style設置爲Storyboard中的Subtitle。 dequeueReusableCellWithIdentifier將返回一個實例化的單元格。

3
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
          reuseIdentifier:CellIdentifier]; 
4

設置表格視圖單元格樣式在故事板

以字幕和編寫代碼來配置你細胞

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

enter image description here

0

斯威夫特3:

如果你是米

let tableContent = ["1", "2", "3", "4", "5"] 
let tableDetailContent = ["a", "b", "c", "d", ""] 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "yourCellIdentifier") 
    cell.textLabel?.text = tableContent[indexPath.row] 
    cell.detailTextLabel?.text = tableDetailContent[indexPath.row] 

    return cell 
} 

enter image description here

這部分代碼從上面可以讓你設置你的detailTextLabel ..你可以設置任何你想要的故事板的表視圖單元格樣式:與重寫「cellForRowAt indexPath」功能亞慶你的(自定義,基本,右側細節,左側細節,小標題),此行將覆蓋它:

style: UITableViewCellStyle.subtitle