ios - TableView 单元格显示空白

标签 ios parsing cell uitableview

我在 .xib 中有一个 TableView 单元格。我在 .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

这是预期的。

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

这是我的 .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];
}

关于ios - TableView 单元格显示空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31289900/

相关文章:

Java SimpleDateFormat 上午/下午

ios - 自定义表格单元格不会滚动

java - JTable增量单元格填充

Python:Scrapy 导出原始数据而不是仅 text()?

css - 如何使用 CSS 格式化 XML 文件?

html - 文本在 1cm x 1cm 单元格中居中

ios - 将项目升级到 Xcode 8

ios - 无法使用 '[APISKeyObjects]' 类型的索引为 'String' 类型的值添加下标吗?

objective-c - 使用另一个项目的 Storyboard

ios - Swift NSMutableDictionary 删除对象