2015-01-07 43 views
1

我希望每個單元都有detailTextLabel顯示。iOS UITableViewCell detailTextLabel不顯示

細胞被實例化(在cellForRowAtIndexPath:)有:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

而且我tryed設置樣式類型有:

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

(我的Xcode給ARC與自動發佈警告,所以我也嘗試過它省略。雖然相同的結果)

我有點困惑。顯然,如果沒有cell == nil,代碼的第一部分是徒勞的,但對於它來說,單元格從來沒有detailTextLabel顯示。 (是的,cell.detailTextLabel.text設置正確)

我該怎麼辦呢?

更新:因爲我正在使用故事板,我能夠通過將單元格樣式設置爲'副標題'來實現所需的結果。然而,如何編程的這個問題仍然存在

+0

如果您啓用了ARC而不是'autorelease',則不需要。 – Kampai

+0

在這個地方使用自定義單元格 –

+1

你在使用故事板嗎?如果是的話,看看這個線程 http://stackoverflow.com/questions/15424453/why-is-my-uitableviewcell-not-showing-detailtextlabel-in-any-style – riyaz

回答

0

更改爲下面的代碼應該在編程時執行此操作。 (感謝確實到mbm29414)

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
            reuseIdentifier:@"Cell"]; 
} 
0

創建表格單元格的現代途登記單元格,然後用食指路徑出隊了。問題是,如果您註冊UITableViewCell,您將永遠得到default類型的單元格。

解決方案是子類UITableViewCell並在其中設置樣式。例如:

class SubtitleTableViewCell: UITableViewCell { 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) 
    } 

    required init?(coder aDecoder: NSCoder) { 
    fatalError() 
    } 
} 

現在,在註冊時使用您的子類。

let table = UITableView(frame: .zero, style: .plain) 
table.register(DebugTableViewCell.self, forCellReuseIdentifier: identifier) 
相關問題