【问题标题】:UITableViewCell : prevent cell reloadingUITableViewCell : 防止单元格重新加载
【发布时间】:2014-07-24 04:03:17
【问题描述】:

我有一个UITableView,我是这样填写的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyData *myData = [arrayData objectAtIndex:indexPath.row];
    MyTableViewCell *cell = [[MyTableViewCell alloc] init:myData.title
                                                         :myData.url];
    return cell;
}

MyTableViewCell中有一个UIImageView,我是这样加载的:

- (id) init : (NSString *) parTitle : (NSString *) parUrl {
    // ...
    [self load:parUrl];
    // ...
}

- (void) load : (NSString *) url {
    NSURL *myURL = [NSURL URLWithString:url];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 18, 70, 65)];
        NSData *imageData = [NSData dataWithContentsOfURL:myURL];
        dispatch_async(dispatch_get_main_queue(), ^{
            // Update the UI
            [image setImage:[UIImage imageWithData:imageData]];
            image.contentMode = UIViewContentModeScaleAspectFit;
            [self.contentView addSubview:image];
            [self reloadInputViews];
        });
    });
}

问题是每次一个单元格从屏幕上消失时,当我向后滚动时它会重新加载其内容,但我不想要它。该怎么办?感谢您的帮助。

【问题讨论】:

  • 它被称为细胞重用,你确实想要那个

标签: ios objective-c uitableview


【解决方案1】:

您只需要一个更复杂的解决方案。将下载的图像存储在模型中并预先检查,这样如果图像已经下载,则无需再次下载。

【讨论】:

【解决方案2】:
  • 我认为缺少的部分是 [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; 始终重复使用单元格。
  • 您可以缓存图像,有很多方法可以为 iO 缓存图像。 Best way to cache images on ios app?

【讨论】:

  • 他现在不想缓存它们......我承认这是一个不好的方法
  • 基本问题是:单元格不是模型对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多