2011-10-30 22 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/

最后一段的解决办法

相关问题