【发布时间】:2013-02-22 22:26:30
【问题描述】:
在我的应用程序中,我正在解析 JSON 数据,然后在 UITableView 中显示该数据。信息显示在表格中,但触摸响应非常糟糕。我做了一些研究,发现建议对信息,尤其是图像实现异步加载,但我找不到任何适用于我的 JSON 应用程序的相关解决方案。我会很感激一些关于如何解决这个问题的建议和cmets,这里是代码:
jURL 定义 www.website.com/info.json
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(jQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
jURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:NO];
});
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* jsonDict = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
calRes = [jsonDict objectForKey:@"results"];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *calDict = [calRes objectAtIndex:indexPath.row];
NSURL *imageURL = [NSURL URLWithString:[calDict objectForKey:@"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.textLabel.text = [calDict objectForKey:@"name"];
cell.detailTextLabel.text = [calDict objectForKey:@"description"];
cell.imageView.image = imageLoad;
return cell;
}
【问题讨论】:
-
延迟是由方法
cellForRowAtIndexPath里面的图片下载给出的,看看this project异步下载图片
标签: ios json uitableview