我对表视图单元格有疑问。
在单元格中,我有一个UIImageView
。此视图从具有getter的Web服务加载:
- (UIImageView *)imageView
{
if (!_ imageView)
{
_imageView = [[TurnipImageView alloc] init];
_imageView.backgroundColor = [UIColor clearColor];
}
[_imageView loadImageViewFromWebService];
return _imageView;
}
当我在[_imageView loadImageViewFromWebService];
语句中添加if (!_ imageView)
调用时,图片视图未正确加载。
当我滚动表视图时,单元格正在重新加载以及图像视图并导致滚动滞后。
也许有人知道如何优化这个过程?
答案 0 :(得分:0)
你应该尝试延迟加载表视图: 以下是示例:https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html
还看看这个: Lazy loading UITableView with multiple images in each cell
答案 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;
}
希望这有帮助