在我的應用程序中,我正在解析JSON數據,然後在UITableView中顯示該數據。信息顯示在表格中,但觸摸響應相當滯後。我做了一些研究,發現建議爲信息和特別是圖像實現異步加載,但是我找不到與我的JSON應用程序一起工作的任何相關解決方案。我希望得到一些建議,如何來解決這個問題的意見,這裏是代碼:顯示JSON數據的UITableView中的響應滯後
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](https://github.com/rs/SDWebImage)以異步方式下載圖像 – tkanzakic 2013-02-22 22:35:00