【发布时间】:2014-12-02 00:43:19
【问题描述】:
当我从本地 JSON 加载时,我的 UITableView 滚动非常缓慢。正在从外部 URL 加载图像。我首先尝试在我的 viewWillAppear 方法中加载 JSON:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HomePage" ofType:@"json"];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile:filePath];
self.titleLabels = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
});
在我的 tableView (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath 中,我有以下内容:
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil) {
[tableView registerNib:[UINib nibWithNibName:@"HomeCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
}
NSDictionary *titleLabels = [self.titleLabels objectAtIndex:indexPath.row];
NSString *label = [titleLabels objectForKey:@"Heading"];
NSURL *imageURL = [NSURL URLWithString:[titleLabels objectForKey:@"Image"]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
cell.label.text = label;
cell.imageView.image = image;
cell.cardView.frame = CGRectMake(10, 5, 300, 395);
return cell;
我只是想知道为什么表格视图滚动得很慢。如果有人能对此有所了解,将不胜感激。
谢谢!
【问题讨论】:
-
这行
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];导致了这个问题。这是来自主线程的同步调用,因此它会阻塞 UI,直到检索到图像数据。对图片使用延迟加载
标签: ios objective-c json uitableview