以编程方式设置单元标识符

时间:2014-12-30 16:03:23

标签: ios uitableview

我正在构建一个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;
}

我没有在这款应用中使用故事板。

1 个答案:

答案 0 :(得分:0)

为了使用dequeueReusableCellWithIdentifier:forIndexPath:(而不仅仅是dequeueReusableCellWithIdentifier:),需要首先使用表视图注册该重用ID,并使用{}之一来引用单元类或xib。 {3}}

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