【问题标题】:cellForRowAtIndexPath: Is it XCode template correct?cellForRowAtIndexPath:XCode 模板是否正确?
【发布时间】:2012-07-27 09:34:59
【问题描述】:

这个问题是关于 cellForRowAtIndexPath。

据我所知正确的方法是

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 模板是否正确?

【问题讨论】:

标签: ios xcode uitableview


【解决方案1】:

模板代码正确。

从 iOS 5.0 开始,可以为单元格标识符注册一个 nib。每当表格需要一个单元格并且没有剩余可重复使用的单元格时,它都会实例化 nib 并自行创建单元格。

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

注册 nib 的 API 为:registerNib:forCellReuseIdentifier:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-11
    • 2021-09-12
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多