【问题标题】:Image did not load into cell - IOS图像未加载到单元格 - IOS
【发布时间】:2016-02-15 20:40:10
【问题描述】:

我正在尝试将图像加载到表格视图单元格中,第一次图像不显示,但如果我刷新表格(使用刷新控件)并重新加载数据并且图像完美显示。

**我正在将图像加载到主线程中,并且我正在使用自定义单元格

UIImage *image = //load image, it always return a value, never nil

if(image != nil){
    self.iconCell.contentMode = UIViewContentModeScaleAspectFill;
    self.iconCell.clipsToBounds = YES;
    self.iconCell.image = image;

    [self.iconCell setNeedsDisplay];
}

这个问题的根源可能是什么?

【问题讨论】:

  • 显示如何将图像加载到表格视图单元格中的代码...
  • @MichaelDautermann 我更新了问题。
  • iconCell 是一个自定义单元格。它是否有 image 属性或其他指向图像视图的属性?
  • 它是一个自定义单元格类,具有 UIImageView 属性 calle iconCell
  • 你在哪里实例化 UITableView,或者你在哪里创建与你发布的代码相关的自定义单元格?

标签: ios iphone tableview


【解决方案1】:

作为自定义表格视图单元格的一部分,您应该跟踪自定义单元格当前显示的行(或索引路径)。

例如向自定义单元格添加两个属性,例如:

@property (strong) NSIndexPath *currentIndexPath;
@property (weak) UITableView *parentTableView; // important, must be weak!

然后您可以在视图控制器的“cellForIndexPath:”方法中设置它,例如:

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    ...
    myCustomCell.currentIndexPath = indexPath;
    myCustomCell.parentTableView = tableView;

现在,在更新单元格中的图像的同一函数中(以及之后),您可以通过执行以下操作重新加载单个单元格:

UIImage *image = //load image, it always return a value, never nil

if(image != nil){
    self.iconCell.contentMode = UIViewContentModeScaleAspectFill;
    self.iconCell.clipsToBounds = YES;
    self.iconCell.image = image;
}

// reloading now should have the image properly set...
NSArray *rowToReload = [NSArray arrayWithObject:self.currentIndexPath];
[self.parentTableView reloadRowsAtIndexPaths:rowToReload withRowAnimation:UITableViewRowAnimationNone];

更多信息可以看in this related question

【讨论】:

    【解决方案2】:

    问题是我正在使用自动布局,并且视图没有安装检查我正在测试的当前尺寸。

    Installing and Uninstalling Views for a Size Class

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 2021-01-31
      • 2011-07-09
      • 2023-04-03
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多