表格单元格未连接到iOS故事板中的插座

时间:2013-02-17 23:24:59

标签: ios uitableview uistoryboard

我关注Connect outlet of a Cell Prototype in a storyboard

所以我的网点连接起来了。我有这个方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] ;
    }

    // Configure the cell.
    Group * group = [self.groups objectAtIndex: [indexPath section]];
    Friend  * friend = [[group friends ]objectAtIndex: [indexPath row]];
    NSLog(@"%@", friend.name);

    [[cell nameLabel] setText:friend.name];
    [[cell statusLabel] setText:friend.status];
    [[cell picImageView] setImage:[UIImage imageNamed:@"similey.jpg"]];
    return cell;
}

它编译得很好,但细胞显示空白,没有任何内容。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您是否将故事板中单元格的标识符设置为“Cell”以匹配您的dequeueReusableCellWithIdentifier调用?

Interface Builder创建对象,然后允许您复制它们。它不会改变类的定义。因此,如果您在Interface Builder中设计一个类的实例,那么您只需要设计该单个实例。加载Storyboard和NIB是可用于复制该实例的机制。

在IB中设计一个类的实例,然后再去做该类的直接alloc + init将导致一个实例完全独立于Interface Builder中的实例。

以下代码行向我表明,您希望故事板中的实例能够对直接分配+ init您的类时会发生什么影响:

[[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]

如果你曾经遇到那条线,那可能是你的问题。如果您在故事板中创建了单元格,则该行不应该存在。对于故事板中创建的单元格,-dequeueReusableCellWithIdentifier:将始终返回单元格的实例,前提是您匹配标识符。

答案 1 :(得分:-2)

由于HomeCell是一个自定义的UITableViewCell,它不会处于重用队列中,所以你的表视图需要通过registerClass方法来了解它,如下所示:

- (void)viewWillAppear:(BOOL)animated
{
   ...
   [self.tableView registerClass:[HomeCell class] forCellReuseIdentifier:@"Cell"];

}