在滚动时加载新图像时,表视图会急剧移动

时间:2015-12-19 20:46:46

标签: ios objective-c

我有从服务器加载jSON数据的自定义类,当加载该数据时,我使用AFNetworking加载每个单元格的图像。但是,当我向下滚动到要加载的新图像时,存在令人讨厌的锐化效果,而不是平滑滚动。有我的方法:

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

    //Create cell

    static NSString *cellIdentifier = @"cell";

    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCellView" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    // Check condition, either we got objects

    if (!isDataLoaded){

        cell.myActivityIndicator = [[UIActivityIndicatorView alloc]
                        initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

        cell.myActivityIndicator.center=self.view.center;
        [cell.myActivityIndicator startAnimating];
        [self.view addSubview:cell.myActivityIndicator];

        NSLog(@"Current loading");

    }   else {

        // Hide acitivity indicator



        // Set image

//            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
//                
//                 data = [[NSData alloc] initWithContentsOfURL:[[self.objectsToShow objectAtIndex:indexPath.row]valueForKey:@"low_resolutionImage"]];
//                    UIImage *image = [[UIImage alloc] initWithData:data];
//                
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        
//                        cell.myActivityIndicator.hidden = YES;
//                        [cell.myActivityIndicator removeFromSuperview];
//                        
//                        [cell.myImageView setImage:image];
//                    });
//                
//            });

        [cell.myImageView setImageWithURL:[[self.objectsToShow objectAtIndex:indexPath.row]valueForKey:@"low_resolutionImage"]];

    }

    return cell;
}

1 个答案:

答案 0 :(得分:1)

每次表格单元格即将展示时,您都不想下载(异步下载必须)图像。而是在第一次下载图像后将其保存在数据模型中。之后,您只需从模型中检索图像即可。我通常使用NSMutableArray来实现模型,因为它更容易添加/删除其元素。