2013-02-17 25 views
0

我跟着Connect outlet of a Cell Prototype in a storyboard表格单元不连接到iOS故事板的插座

所以我的插座已连接。我有这种方法:

- (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; 
} 

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

+0

您是否将故事版中的单元格类更改为HomeCell?数据源方法是否被调用? – rdelmar 2013-02-17 23:44:30

+0

检查调试器中的单元格是否正确,并且单元格项不为零。 'NSLog(@“cell class:%@”,NSStringFromClass([cell class]));' – zaph 2013-02-18 00:02:48

+0

@Zaph它显示homecell 和故事板中的类显示HomeCell以及... – SuperString 2013-02-18 00:30:51

回答

-2

由于HomeCell是一个自定义的UITableViewCell它不会在重用队列,所以你的表视图将需要通过的registerClass方法来了解它,像这样:

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

} 
+2

这不会导致使用故事板中的表格单元格。 – 2013-02-20 20:46:41

+1

在故事板中使用原型单元时,这是不正确的。看到这个原因http://stackoverflow.com/questions/16503874/prototype-cell-from-storyboard-not-creating-uilabels-etc – anders 2014-12-18 15:58:38

+0

作者询问有关从故事板设置插座...您正在描述如何注册以编程方式将类作为重用标识符的表格单元格。这是通过在原型单元上设置重用标识符在故事板中完成的。 – Christoph 2017-03-27 14:42:20

0

你设置标识符故事板中单元格的“Cell”与您的dequeueReusableCellWithIdentifier调用相匹配?

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

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

这下面的代码行表示,我认为你期待的实例在故事板会对当你的Alloc +直接初始化类会发生什么影响:

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

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