当桌面视图出现时,首先没有图像。但是当我拖动它时,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]];
答案 0 :(得分:2)
在为imageView设置图像之前将单元格imageView.image
设置为nil
,因为tableView每次滚动时都会重新加载单元格。
设置UITableViewCell的imageView.image
调用setNeedsDisplay
和reloadInputViews
后立即设置图像。
[cell reloadInputViews];
[cell setNeedsDisplay];
希望它有所帮助。