나는 도메인 개체를 기반으로 내있는 tableview에서 행을 제거하는 방법을 구현하고있어하지만 난 전화 deleteRowsAtIndexPath 후 오류를 받고 있어요 : 애니메이션UITableView deleteRowsAtIndexPath 모델 업데이트시기
인용문 종단 응용 프로그램으로 인해 캐치되지 않는 예외 'NSInternalInconsistencyException', 이유에 : '잘못된 업데이트 : 섹션 0의 행 수가 잘못되었습니다. 업데이트 (2) 후에 기존 섹션에 포함 된 행 수는 업데이트 (2) 이전에 해당 섹션에 포함 된 행 수와 같아야합니다 (더하기 또는 빼기 해당 섹션에서 삽입되거나 삭제 된 행의 수 (삽입 된 0, 삭제 된 1). '
그래서 "나는 모델을 너무 빨리 업데이트 중"이라고 생각했습니다. 코드 :
- (void)deleteItem:(Item *)item {
NSInteger index = [items indexOfObject:item]; // items is the model a NSArray
if (index != NSNotFound) {
NSMutableArray *itemEditable = [items mutableCopy];
[itemEditable removeObjectAtIndex:index];
self.items = itemEditable;
NSIndexPath *indexToDelete = [NSIndexPath indexPathForRow:index inSection:0];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexToDelete] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
그리고 그때 내가 deleteRowsAtIndexPath 전화 후 모델 를 업데이트하는 코드를 변경 :
- (void)deleteItem:(Item *)item { NSInteger index = [items indexOfObject:item]; // items is the model a NSArray if (index != NSNotFound) { NSIndexPath *indexToDelete = [NSIndexPath indexPathForRow:index inSection:0]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexToDelete] withRowAnimation:UITableViewRowAnimationMiddle]; NSMutableArray *itemEditable = [items mutableCopy]; [itemEditable removeObjectAtIndex:index]; self.items = itemEditable; } }
애니메이션하지만 해결되지 않았다 변경하는 유일한 번호입니다 오류 메시지의 행 수를 2에서 3으로 변경하십시오.
차단 인용문 캐치되지 않은 예외 'NSInternalInconsistencyException'으로 인해 앱 종료 중, 잘못된 이유 : '잘못된 업데이트 : 잘못된 번호 update (3) 이후의 기존 섹션에 포함 된 행 수는 업데이트 (3) 이전에 해당 섹션에 포함 된 행 수와 같거나 삽입되거나 삭제 된 행 수를 더한 값과 같아야합니다 그 섹션에서 (0 삽입, 1 삭제). '
어디에서 모델을 업데이트해야합니까?
내가 문제를 발견했습니다
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [items count];
}
deleteRowsAtIndexPath 전에 모델을 업데이트해야합니다. numberOfRowsInSection 메서드를 게시 할 수 있습니까? – Anna
물론 할 수 있습니다. –