【问题标题】:Set cell identifier programmatically [duplicate]以编程方式设置单元标识符[重复]
【发布时间】:2015-02-26 19:18:29
【问题描述】:

我正在构建一个UITableView,其中包含一些单元格。但是,它一直在抛出unable to dequeue a cell with identifier cell。我正在使用下面的代码,它应该可以工作。我做错了什么?

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

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

    cell.textLabel.text = [self.ninjas[indexPath.row] name];

    NSString *imageUrl = [NSString stringWithFormat: @"%@", [self.ninjas[indexPath.row] thumbnail]];

    [cell.imageView setImageWithURL:[NSURL URLWithString:imageUrl]
                   placeholderImage:[UIImage imageNamed:@"50-50.jpg"]];

    return cell;
}

我没有为此应用使用情节提要。

【问题讨论】:

标签: ios uitableview


【解决方案1】:

为了使用dequeueReusableCellWithIdentifier:forIndexPath:(而不仅仅是dequeueReusableCellWithIdentifier:),需要首先使用these functions之一向表格视图和对单元格类或xib的引用注册该重用ID

编辑:此外,如果您使用 Interface Builder 创建原型单元格,您还需要为这些单元格指定一个resue ID。

【讨论】:

  • 是的,很抱歉,我之前应该提到过这一点,但我没有为这个应用程序使用故事板。
  • 然后在创建UITableView 后简单地注册您的UITableViewCell 为您的重用ID 应该可以解决您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-12
  • 2016-12-11
  • 2012-07-05
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多