自定义uitableviewcell不会显示标签文本

时间:2013-03-02 14:14:26

标签: objective-c cocoa-touch uitableview

标签的值也是null。

我不确定发生了什么。

这些是我的课程/代码

@interface CustomEventCell : UITableViewCell{

}

@property (nonatomic, weak) IBOutlet UILabel *participant1label;
@property (nonatomic, weak) IBOutlet UILabel *participant2label;
@property (nonatomic, weak) IBOutlet UILabel *status;

@end

事件模型是

@interface Event : NSObject{
}
@property NSNumber *matchId;
@property NSString *participant1, *participant2,
-(id)init;
@end

这是填充单元格的tableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"EventCell";

    CustomEventCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[CustomEventCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // events is a NSMutableArray with Event instances
    Event *event = [events objectAtIndex:indexPath.row];

    cell.participant1label.text = event.participant1;

    return cell;
   }

这是我的设置

enter image description here enter image description here

enter image description here

enter image description here

我必须遗漏一些东西,因为我有另一个uitableview,它可以毫无问题地填充自定义。我比较了它们,它们是相同的。我甚至尝试回到常规标签,它会填满,但不是这个自定义标签。

编辑:

修改了错误的复制代码。

1 个答案:

答案 0 :(得分:1)

我写了一个简单的小测试应用程序试图复制你想要看到的工作。我使用Storyboard将其设为here。如果你无法弄清楚这一点,那么也许你可以拿测试应用程序并对其进行修改,以便它复制你所看到的不良行为。

我最好的猜测是,当你初始化你的单元格时,它没有连接到xib中的视图。

尝试在以下位置设置断点:

cell.participant1label.text = event.participant1;

并通过执行以下操作验证cell.participant1label不是nil:

NSLog( @"cell.participant1label: %@", cell.participant1label );

我有一个小错误,其中没有我的自定义标签出现,cell.participant1label为零。原因是我没有将自定义表格视图单元格的标识符设置为“EventCell”。因此,我可能会建议重新检查并确保您的代码和XIB之间的标识符确实匹配。

相关问题