ios - tableviewcell 中的图像不会立即出现

标签 ios objective-c uitableview cocoa-touch

当 tableview 出现时,起初没有图像。但是当我拖动它时,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后立即调用UITableViewCell的setNeedsDisplayreloadInputViews设置image。

[cell reloadInputViews];                
[cell setNeedsDisplay];

希望对您有所帮助。

关于ios - tableviewcell 中的图像不会立即出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38435482/

相关文章:

ios - 是否可以子类化 UITableView 并使其具有基于单元格数量的固有内容大小?

ios - EKEventEditViewController 打开速度非常慢

iOS Tableview 文本字段在 for 循环中获取值在 ios 7 中不起作用,但在 iOS 8 中起作用

android - 在 flutter 中显示全屏加载小部件

ios - 如何将从该请求收到的数据或 json 附加到空数组?

ios - viewDidAppear 出现在代码中会扰乱布局

ios - 扩展或自定义 cocoapods 的正确方法

ios - 底部UITableView隐藏时如何让UIScrollView占据屏幕高度?

ios - 如何将填充添加到搜索栏 swift ios

ios - 打破SwiftUI中List的最大容量