2012-07-27 69 views
0

这个问题是关于cellForRowAtIndexPath。cellForRowAtIndexPath:XCode模板是否正确?

当我知道正确的方法是

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
if (cell == nil) { 
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"]; 
return cell; 

但是,当我创建主从模板,代码看起来像这样

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
[self configureCell:cell atIndexPath:indexPath]; 
return cell; 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
cell.textLabel.text = [[object valueForKey:@"timeStamp"] description]; 
} 

编辑:现在的问题是不是configureCell atIndexPath。正如您在XCode模板中看到这些字符串不存在一样。

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:cellIdentifier] ; 

是XCode的模板是否正确?

+0

检查这个,它会给你答案:http://stackoverflow.com/questions/5467831/why-do-programmers-use-configurecellatindexpath-method-to-config-the-tableview – Dhruv 2012-07-27 09:45:03

回答

4

模板代码是正确的。

从iOS 5.0开始,可以为单元标识符注册一个笔尖。无论何时表需要一个单元并且没有可重用的单元,它都会实例化该单元并自行创建单元。

如果你这样做dequeueReusableCellWithIdentifier:总是返回一个单元;你不必自己创建它。

注册笔尖的API是:registerNib:forCellReuseIdentifier: