【问题标题】:The difference between two ways to access UIImageView under UITableViewCell of UITableViewUITableView的UITableViewCell下访问UIImageView的两种方式的区别
【发布时间】:2012-02-14 16:43:44
【问题描述】:

我有一个自定义 tableview 来通过 web 服务加载新闻。我在方法 cellForRowAtIndexPath 中更改每个单元格中的图像。获得正确的单元格后,我可以通过两种方式访问​​图像:

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
    imageView.image = news.image;

或直接:

    cell.imageView.image = news.image;

当我使用 第二种 方式时,一些图像进入错误的单元格,而第一种方式给了我一个完美的结果。它也发生在回调方法iconDownLoadFinsh中。

我对此感到困惑。任何人都可以尝试解释一下吗?请不要犹豫,提出问题。提前致谢。

- (void)iconDownLoadFinsh:(NSData *)imageData row:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
    imageView.image = [UIImage imageWithData:imageData];
    NewsModel *newsModel = [newsArray objectAtIndex:indexPath.row];
    newsModel.image = [UIImage imageWithData:imageData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NewsCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"NewsTableViewCell" owner:self options:nil];
        cell = newsTableViewCell;
        newsTableViewCell = nil;
    }

    // read from newsModel
    NewsModel *news = [newsArray objectAtIndex:indexPath.row];

    UILabel *label;
    label = (UILabel *)[cell viewWithTag:10];
    label.text = [NSString stringWithFormat:news.title];
    label = (UILabel *)[cell viewWithTag:11];
    label.text = [NSString stringWithFormat:news.description];
    UIImageView *imageView;
    imageView = (UIImageView *)[cell viewWithTag:12];
    imageView.image = news.image;

    if (news.image == nil)
    {
        imageView.image = [UIImage imageNamed:IconPlaceHolder];

//      if (self.tableView.dragging == NO && self.tableView.decelerating == NO) {

            IconDownLoader *iconDownLoader = [[IconDownLoader alloc] init];
            iconDownLoader.url = news.imageUrl;
            iconDownLoader.delegate = self;
            iconDownLoader.indexPath = indexPath;
            [downloadArray addObject:iconDownLoader];
            [iconDownLoader start];
//      }
    }

    return cell;
}

【问题讨论】:

    标签: iphone ios uitableview reusability


    【解决方案1】:

    我一般总是在方法中重置单元格属性

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    所以我拿到单元格后会做如下操作

    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"NewsTableViewCell" owner:self options:nil];
        cell = newsTableViewCell;
        newsTableViewCell = nil;
    }
    cell.imageView.image = nil; // Here resetting the imageview's image to nil.
    

    因为你在重复使用单元格,如果你不清除旧数据,单元格数据可能会损坏。

    【讨论】:

    • 嗨@mbh 感谢您的建议。这样做很好。但似乎对这个问题没有贡献。我试过了,但有些图像仍然叠加到其他图像上。 **似乎cell.imageView.image在快速滚动时找不到正确的单元格**
    • 理想情况下我不会在快速滚动时加载图像。当滚动停止时,我会懒惰地加载图像。建议通过 Apple 的 LazyTableLoad(某种名称)示例代码。您将为您自己和您的应用带来很大的帮助。
    • 刚刚看到您实际上是在使用延迟加载代码!竖起大拇指。现在取消注释您注释掉的那一行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多