【问题标题】:Image in tableviewcell not appearing immediatelytableviewcell 中的图像不会立即出现
【发布时间】:2016-07-18 11:24:15
【问题描述】:

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

【问题讨论】:

  • 你永远不应该在一个不是初始化器的方法前面加上init
  • 谢谢你的建议。我会纠正它。但它不能解决我的问题
  • 这就是为什么我没有将其发布为答案。

标签: ios objective-c uitableview cocoa-touch


【解决方案1】:

在为 imageView 设置图像之前,将单元格的 imageView.image 设置为 nil,因为 tableView 每次滚动时都会重新加载单元格。 设置imageView.image 后立即调用UITableViewCell 的setNeedsDisplayreloadInputViews 设置图像。

[cell reloadInputViews];                
[cell setNeedsDisplay];

希望对你有帮助。

【讨论】:

  • 很抱歉我没有写清楚。 imageview的初始化在viewdidload中。 imageview 有实例,在 tableview 出现之前。但我仍然感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2013-11-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 2017-07-22
相关资源
最近更新 更多