Table 查看显示空白的单元格

Table View Cells showing blanks

我在 .xib 中有一个 Table 查看单元格。我在 .xib 中有一个 UILabel 和 UIImageView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{   
    SchoolsTableViewCell *cell = (SchoolsTableViewCell *) [[[NSBundle mainBundle] loadNibNamed:@"SchoolsTableViewCell" owner:self options:nil] lastObject];

    NSLog(@"Before: %@", cell.nameLabel.text);
    cell.nameLabel.text = [NSString stringWithFormat:@"Name: %@", [object objectForKey:@"name"]];
    NSLog(@"After: %@", cell.nameLabel.text);

    PFFile *file = [object objectForKey:@"logo"];
    [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error)
     {
         cell.logoImageView.image = [UIImage imageWithData:data];
     }];

    return cell;
}

如你所见,我有 2 个 NSLog()s,打印如下:

2015-07-08 20:17:26.739 o [1871:108786] Before: (null)

2015-07-08 20:17:26.740 o [1871:108786] After: Name: SMSAS

这是预期的。

但是,在我的模拟器上,我只能看到名称为:的空白单元格。它应该在标签中显示 SMSAS,并加载徽标,但它没有发生。我可以在点击单元格时将对象传递给下一个视图控制器,它可以在那个视图控制器中显示。

这是我的单元格的 .m。

@implementation SchoolsTableViewCell

- (void)awakeFromNib
{
    [super awakeFromNib];

    self.nameLabel = [[UILabel alloc] init];
    self.logoImageView = [[UIImageView alloc] init];

    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.logoImageView];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end

我确定这是一个简单的错误。有人愿意帮我开开眼吗?谢谢!

使用dequeueReusableCellWithIdentifier

static NSString * CellIdentifier = @"SchoolsTableViewCell";
SchoolsTableViewCell *cell = (SchoolsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SchoolsTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}