2015-05-02 39 views
0

我在視圖控制器中有一個表格視圖,當我點擊一個按鈕時,它應該在線搜索數據並填入表格視圖單元格。出於某種原因,當我運行以下代碼時,加載表視圖需要大約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]; 
       } 
      } 
     }); 
    } 
} 

回答

0

您正在重新加載後臺線程中的tableView。 刷新了的tableView,以及在主線程中執行所有的用戶界面相關的任務:

- (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]; 

       dispatch_async(dispatch_get_main_queue(),^
      { 
       if ([trackName count] == 0) { 
         [self.activityIndicator stopAnimating]; 
         [self.tableView setHidden:YES]; 
         [self.noResultsLabel setHidden:NO]; 
        } 
        else { 
         [self.tableView reloadData]; 
         [self.cancelButton setHidden:YES]; 
        } 
      } 
      } 
     }); 
    } 
} 
+0

這是有效的。一個問題,但。我點擊搜索按鈕,它說沒有結果。然後我再次打開它並正確加載。順便說一句,我把代碼放在'});'後面的代碼中。 – smecperson

+0

我把'});'之前的代碼,它現在完美。謝謝! – smecperson

+0

不客氣。不要忘記接受我的回答:) –

0

穿上主線程的UI相關的工作將幫助你實現你試圖做 //一旦你得到一個回調從服務器和您完成解析數據,呼叫以下內部您的外部調度塊

dispatch_async(dispatch_get_mainQueue(), ^{ 
// do your UI work here eq reloading tabelView 
     [self.tableView reloadData]; 
});