2014-11-19 1 views
0

블록 내에서 tableView의 단일 섹션에 행을 추가/추가하려고합니다. 문제는 인덱스 경로 (insertRowsAtIndexPaths)를 추가하면 코드가 충돌합니다 (업데이트 (12) 이후에 기존 섹션에 포함 된 행 수는 업데이트 전에 해당 섹션에 포함 된 행 수와 같아야합니다.) yadayadayada) 블록 내에서 tableView에 행을 추가합니다.

가 지금은 다시 [weakSelf.tableView numberOfRowsInSection가 : 0]는 사실을 추적 나는 또한 weakSelf를 사용하여 데이터 소스()에 추가 데이터를 추가 한 후 업데이트되지는

사람은 무엇을 알고 나는 여기에서 놓치고있다?

__weak typeof(self) weakSelf = self; 

_tipSearchController.searchResultBlock = ^(NSArray *tips, BOOL firstBatch) { 
    weakSelf.noLocationsAfterSearchFilters = NO; 
    if (firstBatch==YES) { 
     [weakSelf.tips removeAllObjects]; // empty datasource if required 
     [weakSelf.tableView reloadData]; 
    } 

    NSUInteger currentTipCount=weakSelf.tips.count; 
    [weakSelf.tips addObjectsFromArray:tips]; // adding data to datasource. 

    NSMutableArray *indexPaths = [NSMutableArray array]; 
    for (NSUInteger i = 0; i < tips.count; ++i) { 
     [indexPaths addObject:[NSIndexPath indexPathForRow:i+currentTipCount inSection:0]]; 
    } 

    // Crash here ;(
    [weakSelf.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; 

}; 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.tips.count; 
} 
+0

주 스레드에서이 블록 실행합니까 –

+2

당신의 numberOfRowsAtIndexPath 방법을 게시하시기 바랍니다? 그렇지 않은 경우 데이터 원본 및 tableViewInsertion의 업데이트를 주 스레드에 디스패치하십시오. – mbo42

+0

그래, 주요 위협에서 실행하지만 어떻게 든 "numberOfRowsInSection"자체 (weakSelf 제안) .... 호출되지 않습니다 발견 –

답변

0

먼저 @mbo42와 같이 주 스레드에서 searchResultBlock이 실행되도록하십시오. beginUpdates/endUpdates에서 둘째, 랩 행 삽입 :

[weakSelf.tableView beginUpdates]; 
NSUInteger currentTipCount=weakSelf.tips.count; 
[weakSelf.tips addObjectsFromArray:tips]; // adding data to datasource. 

NSMutableArray *indexPaths = [NSMutableArray array]; 
for (NSUInteger i = 0; i < tips.count; ++i) { 
    [indexPaths addObject:[NSIndexPath indexPathForRow:i+currentTipCount inSection:0]]; 
} 

[weakSelf.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; 
[weakSelf.tableView endUpdates];