我在視圖控制器中有一個表格視圖,當我點擊一個按鈕時,它應該在線搜索數據並填入表格視圖單元格。出於某種原因,當我運行以下代碼時,加載表視圖需要大約10秒鐘的時間。但是,如果我點擊搜索,然後在1秒後滑動表格視圖,它立即加載。有什麼辦法可以讓它立即加載而無需向下滾動嗎?滾動時表格視圖更新更快
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.songField) {
[self.songField resignFirstResponder];
NSString *searchString = self.songField.text;
NSString *encodedSearchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *finalSearchString = [NSString stringWithFormat:@"https://itunes.apple.com/search?term=%@&entity=song&limit=10",encodedSearchString];
NSURL *searchURL = [NSURL URLWithString:finalSearchString];
dispatch_queue_t iTunesQueryQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(iTunesQueryQueue, ^{
NSError *error = nil;
NSData *data = [[NSData alloc] initWithContentsOfURL:searchURL options:NSDataReadingUncached error:&error];
if (data && !error) {
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
trackName = [[JSON valueForKeyPath:@"results.trackName"] mutableCopy];
artistName = [[JSON valueForKeyPath:@"results.artistName"] mutableCopy];
artwork = [[JSON valueForKeyPath:@"results.artworkUrl100"] mutableCopy];
preview = [[JSON valueForKeyPath:@"results.previewUrl"] mutableCopy];
linkData = [[JSON valueForKeyPath:@"results.trackViewUrl"] mutableCopy];
if ([trackName count] == 0) {
[self.activityIndicator stopAnimating];
[self.tableView setHidden:YES];
[self.noResultsLabel setHidden:NO];
}
else {
[self.tableView reloadData];
[self.cancelButton setHidden:YES];
}
}
});
}
}
這是有效的。一個問題,但。我點擊搜索按鈕,它說沒有結果。然後我再次打開它並正確加載。順便說一句,我把代碼放在'});'後面的代碼中。 – smecperson
我把'});'之前的代碼,它現在完美。謝謝! – smecperson
不客氣。不要忘記接受我的回答:) –