簡體   English   中英

tableviewcell中的圖像不會立即顯示

[英]Image in tableviewcell not appearing immediately

當桌面視圖出現時,首先沒有圖像。 但是當我拖動它時,tableviewcells中的圖像將會出現。 我正在使用自定義tableviewcell類。

- (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;
}

這是我的自定義單元類:

- (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;
}

最后,這是我的imageview代碼:

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]];

在為imageView設置圖像之前將單元格的imageView.image設置為nil ,因為tableView每次滾動時都會重新加載單元格。 設置完成后imageView.image調用setNeedsDisplayreloadInputViews的UITableViewCell的立即設置圖像。

[cell reloadInputViews];                
[cell setNeedsDisplay];

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM