简体   繁体   English

表格视图单元格显示空白

[英]Table View Cells showing blanks

I've got a Table View Cell in .xib. 我在.xib中有一个表格视图单元格。 I've got a UILabel and UIImageView in the .xib. 我在.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;
}

As you can see, I have 2 NSLog()s, printing the below: 如您所见,我有2个NSLog(),打印以下内容:

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

That is expected. 这是预期的。

However, on my Simulator, I can only see a blank cell with Name:. 但是,在我的模拟器上,我只能看到带有Name:的空白单元格。 It should show SMSAS in the label, and load a logo, but it's not happening. 它应该在标签中显示SMSAS,并加载徽标,但这没有发生。 I can pass the object to the next View Controller when the cell is tapped, and it can show in that View Controller. 轻按单元格时,可以将对象传递给下一个View Controller,并且可以在该View Controller中显示该对象。

This is my .m for the cell. 这是我的单元格.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

I'm sure it's a simple mistake. 我敢肯定这是一个简单的错误。 Anyone care to open my eyes? 有人在睁开我的眼睛吗? Thanks! 谢谢!

Use dequeueReusableCellWithIdentifier 使用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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM