iOS UITableViewCell 重载优化

iOS UITableViewCell reload optimisation

我有一个关于 table 视图单元格的问题。

在单元格中我有一个 UIImageView。此视图通过 getter:

从 Web 服务加载
- (UIImageView *)imageView
{
    if (!_ imageView)
    {
        _imageView = [[TurnipImageView alloc] init];

        _imageView.backgroundColor = [UIColor clearColor];
    }

    [_imageView loadImageViewFromWebService];

    return _imageView;
}

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

当我滚动 table 视图时,单元格和图像视图都在重新加载,导致滚动延迟。

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

您应该尝试延迟加载 table 视图: 这是示例:https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html

也看看这个: Lazy loading UITableView with multiple images in each cell

试试这个

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;
}

希望对您有所帮助