2012-11-10 5 views
2

내 고객이 테이블 뷰 에디션 모드에서 제공 한 빨간색 원을 사용하지 않으므로 앱에 대한 맞춤 삭제 프로세스를 구현하려고합니다. 태그 속성에 행 번호가있는 모든 행에 삭제 버튼을 추가했습니다.프로그래밍 방식으로 UITableView에서 행을 삭제할 수 없습니다.

-(IBAction)deleteRow:(id)sender{ 

    UIButton *tempButton = (UIButton*)sender; 

    [self updateTotals]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tempButton.tag inSection:0]; 

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    [tableView reloadData]; 

    [sharedCompra removeItem:tempButton.tag]; 

    tempButton=nil; 


} 

을 그리고 난에 allways이 오류 : 사용자가 삭제 버튼을 클릭하면, 그것은 다음과 같은 방법을 발사 나는이 코드 조각에 뭔가를 누락하는 경우

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 (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 

그래서 나도 몰라.

감사합니다. [tableView deleteRowsAtIndexPaths... 전에

답변

12

데이터 소스가 원래 상태 (즉, 삭제 전)를 반영하는 반면 행을 삭제하려고합니다. 데이터 소스 을 먼저 업데이트해야합니다 (). 그러면 viev 테이블에서 삭제를 실행할 수 있습니다. 또한 nil에있는 버튼을 설정할 필요가 없으며 테이블보기에 - reloadData를 호출해야하지 않는다 : 당신이해야

- (void)deleteRow:(id)sender 
{ 
    UIButton *tempButton = sender; 
    [self updateTotals]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tempButton.tag inSection:0]; 
    [sharedCompra removeItem:tempButton.tag]; 

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 

(한 가지는, 그러나, 당신이 포맷 방법에주의를 기울이고 당신의 코드)

+0

많은 감사 !!!! – theomen

1

전화 [sharedCompra removeItem:tempButton.tag];[tableView reloadData]

문제는 당신이 모델에서 같은 수를 반환 numberOfRowsInSection를 호출 deleteRowsAtIndexPath:를 호출 할 때이다.

reloadData 여기에서는 호출 할 필요가 없습니다.

1

행을 삭제하고 추가 할 때 beginUpdates를 호출해야합니다. 여기에 어떻게 표시되어야합니다.

[tableView beginUpdates]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[tableView endUpdates]; 

그리고 reloadData를 호출 할 필요가 없으며, deleteRows 및 insertRows 메소드의 애니메이션을 취소합니다. 하지만 데이터를 재설정해야하므로 NSMutableArray를 사용하는 경우 먼저 객체를 제거해야하므로 인덱스 0의 항목을 제거한 다음 테이블에서 행 0을 삭제할 수 있습니다. 행의 수는 삭제를 끝내거나 해당 배열에서 오브젝트의 수와 일치해야합니다.

+0

음, 아니요. ** - beginUpdates와 - endUpdates를 호출 할 필요가 없습니다 **. 문제는 그것이 아닙니다. –

+0

여러 행을 삽입/삭제/다시로드 할 때'beginUpdates'와'endUpdates'를 사용하고 인덱스 경로가 일관되게 유지되어야합니다. 그러나 @ H2CO3이 말했듯이 그것은이 질문과 완전히 관련이 없습니다. – rmaddy

관련 문제