iOS UITableViewCell重新加载优化

时间:2015-02-10 11:09:49

标签: ios objective-c uitableview uiimageview

我对表视图单元格有疑问。

在单元格中,我有一个UIImageView。此视图从具有getter的Web服务加载:

- (UIImageView *)imageView
{
    if (!_ imageView)
    {
        _imageView = [[TurnipImageView alloc] init];

        _imageView.backgroundColor = [UIColor clearColor];
    }

    [_imageView loadImageViewFromWebService];

    return _imageView;
}

当我在[_imageView loadImageViewFromWebService];语句中添加if (!_ imageView)调用时,图片视图未正确加载。

当我滚动表视图时,单元格正在重新加载以及图像视图并导致滚动滞后。

也许有人知道如何优化这个过程?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

试试这个

dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
    //perform download on background thread
    dispatch_async(dispatch_get_main_queue(), ^{
       //assign image to imageview on main thread, UI operations should be carried on main thread
    });
});



- (UIImageView *)imageView
{
    if (!_ imageView)
    {
        _imageView = [[TurnipImageView alloc] init];

        _imageView.backgroundColor = [UIColor clearColor];
    }
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
    //perform download on background thread

        [_imageView loadImageViewFromWebService];
    });
    return _imageView;
}

希望这有帮助