2013-03-08 41 views
3

使用Xcode 4.6,我試圖顯示一個典型的UITableView,其單元格帶有字幕。UITableViewCell字幕。沒有得到它的工作

如果我沒看錯的,代碼是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

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

Test *myTest = [self listTests][indexPath.row]; 

cell.textLabel.text = [myTest name]; 

UIFont *cellFont = [UIFont systemFontOfSize:16.0]; 
cell.textLabel.font = cellFont; 

UIFont *detailFont = [UIFont systemFontOfSize:12.0]; 
NSMutableString *detailText = [NSMutableString stringWithFormat:@"%d", [myTest numQuestions]]; 
[detailText appendString:@" preguntas."]; 

cell.detailTextLabel.text = detailText; 
cell.detailTextLabel.font = detailFont; 

return cell; 

}

出於某種原因,它從來沒有穿過這行代碼:

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

使細胞從不初始化爲UITableViewCellStyleSubtitle

它以某種方式得到總是從一開始有效的細胞做[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

我能是做錯了什麼時候?

它真的很受歡迎。我總是使用這個代碼,它通常工作。我還能在其他地方做錯什麼?

+1

你在IB中使用自定義單元格嗎? – 2013-03-08 11:07:40

+0

你是故事板和原型單元嗎? – katzenhut 2013-03-08 11:07:48

回答

8

當您的單元格使用故事板原型進行定義時會發生這種情況。在這種情況下,可重複使用的單元格是使用initWithCoder:方法預先創建的,因此if (cell == nil)從未被擊中。有關更多信息,請參見this question

由於看來你想使用一個電池與標準的風格,改變了表用故事板原型或設置原型「字幕」應該可以解決這個問題。

+0

+1;我正在自己輸入那個答案。 – 2013-03-08 11:08:17

+1

如果是這樣的話,爲什麼不把SB中的原型單元設置爲「Subtitle」?節省你一些麻煩... – katzenhut 2013-03-08 11:08:58

+0

@ katzenhut這也會工作:)謝謝,我編輯了答案提及。謝謝! – dasblinkenlight 2013-03-08 11:12:49

相關問題