简体   繁体   中英

Image in tableviewcell not appearing immediately

When the tableview appears, at first there are no images. But when I drag it, the images in the tableviewcells will appear. I am using a custom tableviewcell class.

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UserCell  alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
     NSLog(@"%d",indexPath.row);
    [cell setUser:_chatContent[indexPath.row]];
    [cell initUI];
    return cell;
}

This is my custom cell class:

- (void)awakeFromNib {
    [super awakeFromNib];
}
-(BOOL)initUI{
    BOOL isSuccess;
    self.userImageView =  self.user.userImageView;
    UIView *superView = self.contentView;
    [superView addSubview:self.userImageView];
    [self.userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView.mas_top).with.mas_offset(8);
        make.bottom.equalTo(superView.mas_bottom).with.mas_offset(-8);
        make.left.equalTo(superView.mas_left).with.mas_offset(8);
        make.width.equalTo(superView.mas_height).with.mas_offset(-16);
    }];
    self.userImageView.layer.cornerRadius = self.userImageView.frame.size.width/2;
    self.imageView.clipsToBounds = YES;
    isSuccess = YES;
    return isSuccess;
}

Finally, this is my imageview code:

User *user = _chatContent[i];
NSString *fileName = [NSString stringWithFormat:@"%d",i];
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"jpg"];
user.userImageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:filePath]];

Set the cell's imageView.image to nil before setting image for imageView because tableView reloads cells every time it is scrolled. After setting imageView.image call setNeedsDisplay and reloadInputViews of UITableViewCell to set image immediately.

[cell reloadInputViews];                
[cell setNeedsDisplay];

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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