我在 .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/