【问题标题】:asynchronously loaded images in uitableview disappear on scrolling upuitableview 中异步加载的图像在向上滚动时消失
【发布时间】:2014-01-09 06:26:48
【问题描述】:

我能够在 uitableview 上异步获取图像。我正在从 url 获取这些图像。在向上滚动 uitableview 时,这些图像消失了,它们需要时间再次加载,有时它们根本不加载。我不想使用任何 3rd 方库。我不想使用同步方法。请提出任何正确的方法来提高性能。提前感谢帮助。我的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}
UIImageView *imgVw=[[UIImageView alloc]initWithFrame:CGRectMake(250, 10, 40, 30)];
[cell.contentView addSubview:imgVw];
Attributes *att = [listOfObjects objectAtIndex:indexPath.row];
strImgUrl=@"http:image url";
strImgName=att.classifiedImg;
if (strImgName == nil) {
    UIImage *myImg=[UIImage imageNamed:@"user_circle.png"];
    imgVw.image=myImg;
}
else{
    strImg=[strImgUrl stringByAppendingString:strImgName];
}
dispatch_async(dispatch_get_global_queue(0,0), ^{
    NSData *data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:strImg]];
    if ( data == nil )
        return;
    dispatch_async(dispatch_get_main_queue(), ^{

        UIImage *img=[UIImage imageWithData: data];
        imgVw.image=img;
    });

});



return cell;
}

【问题讨论】:

  • 不使用尝试过的party lib意味着你必须写所有你理解和不理解的东西,它不是一个很聪明的方法来找到同一个轮子

标签: ios uitableview uiimageview


【解决方案1】:

UITableViews 旨在重用单元格。例如,当您向上滚动时,第一个单元格可能会被重复使用以显示第 5 个单元格,因为第一个单元格现在不在屏幕上。当您向上滚动时 cellForRowAtIndexPath 再次被调用,并且您再次异步下载相同的图像。如果你想让它立即加载,你需要在第一次下载后缓存图像,这样下次需要该图像时,你可以直接从缓存中拉取它。

许多第三方库都这样做(AFNetworking),但如果您不想使用它们,则必须手动缓存图像。

【讨论】:

    【解决方案2】:

    您的重用代码似乎包含错误,导致每次都创建新单元格。以下代码使用reuseIdentifier作为@"identifier"

    if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
    }
    

    改成:

    if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    

    编辑:

    在重用 tableView 单元格时,您不需要每次都创建子视图并将其添加到单元格。相反,只需在创建单元格时创建子视图,然后如果单元格被重用,只需使用标签获取子视图并从数据源更新您的内容。

    您的代码可以修改如下:

    if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
       UIImageView *imgVw=[[UIImageView alloc]initWithFrame:CGRectMake(250, 10, 40, 30)];
       imgVw.tag = indexPath.row;
       [cell.contentView addSubview:imgVw];
    }
    UIImageView imgVw = (UIImageView*)[cell.contentView viewWithTag:indexPath.row];
    //Rest is same as you posted.
    

    希望这能解决问题。

    【讨论】:

    • 在更改到此数据时在单元格中重叠
    • 一排图像正在改变,其余行为空,没有图像
    • 表示只有一行正在创建?
    • no.rows 是相应创建的,但所有图像都一个接一个地排成一行。它就像图像幻灯片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多