2011-10-30 34 views
9

在iOS5中,在故事板採用ARC和原型細胞的tableView,我可以代替下面的代碼:dequeueReusableCellWithIdentifier行爲已更改爲原型單元格?

static NSString *CellIdentifier = @"Cell"; 

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

// Configure the cell... 
return cell; 

有了這個簡單的代碼??:

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

我看到了這此鏈接:

http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

感謝的提前!

Arildo

回答

8

當然,你的代碼是對的,故事情節automaticaly ALLOC新的細胞,此代碼的工作很大:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    RoadbookCell *cell = (RoadbookCell *)[tableView dequeueReusableCellWithIdentifier:@"RoadbookCell"]; 

    //Configure cell 
    //[cell.lab1 setText:@"Test"]; 

    return cell; 
} 
+1

我不明白爲什麼,但這件事不適合我。我不斷收到一個「無」單元格。我創建了一個新的主 - 細節項目。這個例子很好用。當我添加cellForRowIndexPath方法和表大小方法並將大小設置爲2時,我得到一個異常,因爲dequeueReusableCellWithIdentifier一直讓我「無」。 – bashan

+1

你在故事板中配置了tableCell嗎?像這樣:[鏈接] http://minus.com/m59pfEOqW(注意:單元格標識符與storyboard和cellForRowAtIndexPath中的單元格標識符相同) – Kappe

+2

請記住,您應該在「awakeFromNib」方法的單元格子類中執行任何安裝程序,而不是「initWithStyle:」(它不會被調用),因爲它是從故事板加載的。 – avocade

3

這是蘋果有意它被使用的方式,但我建議反對。有一個錯誤會導致dequeueReusableCellWithIdentifier在設備上啓用VoiceAssist時返回nil。這意味着,您的應用會因啓用此選項的用戶而崩潰。這仍然是作爲的iOS 5.1.1問題

你可以找到更多信息,並在這裏解決方法:

http://hsoienterprises.com/2012/02/05/uitableview-dequeuereusablecellwithidentifier-storyboard-and-voiceover-doesnt-work/

最後一段的解決辦法

相關問題