2012-04-03 1 views
0

insertRowsAtIndexPaths :에 문제가 있습니다. 어떻게 작동하는지 잘 모르겠습니다. WWDC 2010 비디오를 보았지만 여전히 오류가 발생합니다. 나는 모델을 업데이트하기로되어 있다고 생각하고 insertRowsAtIndexPaths를 래핑했다. tableView beginUpdates와 endUpdates 호출에서. 내가 가지고있는 것은 :insertRowsAtIndexPaths가있는 테이블 뷰 업데이트가 잘못되었습니다.

self.customTableArray = (NSMutableArray *)sortedArray; 
[_customTableView beginUpdates]; 
[tempUnsortedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    [sortedArray enumerateObjectsUsingBlock:^(id sortedObj, NSUInteger sortedIdx, BOOL *sortedStop) { 
     if ([obj isEqualToString:sortedObj]) { 
      NSIndexPath *newRow = [NSIndexPath indexPathForRow:sortedIdx inSection:0]; 
      [_customTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newRow] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      *sortedStop = YES; 
     } 
    }]; 
}]; 
[_customTableView endUpdates]; 

customTableArray는 내 모델 배열입니다. sortedArray는 해당 배열의 정렬 된 버전입니다. 더하기 버튼을 눌러 새 행을 추가 할 때이 코드를 실행하면이 오류가 발생합니다.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (2 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

내가 잘못하고있는 것이 확실하지 않습니다. 생각? 감사.

답변

3

배열에서 실제로 어떤 일이 일어나는지 조금 더 분명하게 보길 권합니다. 오류가 읽혀지면서 테이블 뷰에 어떤 이유로 두 행을 추가하라는 메시지가 표시되면서 "행이 하나"라고 말한 다음 -endUpdates 메서드가 호출 된 후 데이터 소스를 확인합니다. 배열에는 총 3 개의 객체가 아니라 2 개의 객체 만 있습니다.

본질적으로 열거 형은 두 개의 삽입을 선택합니다. 배열에는 두 개의 객체가 있습니다. 테이블에는 이미 하나의 객체가 있습니다. 기존 1 + 삽입 2 = 3 행. 배열에는 두 개의 현재 객체 만 있습니다. 여분의 물건은 어떻게 된거야.

나는 어딘가에 두 개의 배열이 동기화되어 있지 않거나 삽입 방법을 평가하는 방식이 어떤 형태의 오류가있을 것으로 예상한다.

도움이 되었기를 바랍니다.