2016-04-12 44 views
0

請考慮下面的代碼:選擇行表導致死機

-(void)selectSpecificRowForSpotlight:(NSNumber*)row{ 
    // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 

     /* Fill news list */ 

     if (self.singlObj.getSpotlightArray.count > 1){ 

      if (self.viewModel.arrValues.count > 1){ 

       NSLog(@"we got spotlight"); 
       [self.viewModel.arrValues insertObject:[[self.singlObj.spotArray objectAtIndex:self.singlObj.currentSpotlightIndex-1] objectAtIndex:0] atIndex:0]; 
       [self.viewModel.arrValues insertObject:[[self.singlObj.spotArray objectAtIndex:self.singlObj.currentSpotlightIndex-1] objectAtIndex:1] atIndex:1]; 
       [self.viewModel.arrValues insertObject:[[self.singlObj.spotArray objectAtIndex:self.singlObj.currentSpotlightIndex-1] objectAtIndex:2] atIndex:2]; 
      } 
     } 
     [self.myTableView reloadData]; 
}); 

    NSIndexPath *path = [NSIndexPath indexPathForRow:[row integerValue] inSection:0]; 
    [self.myTableView.delegate tableView:self.myTableView didSelectRowAtIndexPath:path]; 
} 

細節都無所謂,問題是,下面幾行:

NSIndexPath *path = [NSIndexPath indexPathForRow:[row integerValue] inSection:0]; 
    [self.myTableView.delegate tableView:self.myTableView didSelectRowAtIndexPath:path]; 

原因的死機(SIGABRT錯誤)。我修復它:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 

但當然,它是不好的,由計時器調用一個函數,沒有承諾。爲什麼調用委託方法會導致崩潰?如何避免使用dispatch_after調用?

+0

@vadian如果我需要選擇特定的行修改數值數組後有什麼方法? –

回答

2

NEVER EVER呼叫委託方法開始於或包括will,直接shoulddid。他們僅由目標班級負責。

選擇行存在UITableView

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath 
        animated:(BOOL)animated 
       scrollPosition:(UITableViewScrollPosition)scrollPosition 
+0

我感謝你的幫助。當我用[self.myTableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionTop]替換方法; 它也會導致崩潰。但即使當我使用dispatch_after並且它被調用時,它只會選擇並高亮顯示行(不是在didSelectRowAtIndex中的觸發塊:) –

+0

嘗試在dispatch_async(不是'dispatch_after'!)的主線程上調用'selectRow ...'也許'reloadData' – vadian

+0

它已經在主線程上,因爲dispatch_get_main_queue(),不是? –