2014-05-14 3 views
1

하나의 섹션이있는 tableView에 값 배열이 있습니다. 다음은 행을 삭제하면이 코드가 실행 배열 및 UITableView의 마지막 항목 삭제

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return [allPasses count]; 
} 

처럼 numberOfRowsInSection는 모습입니다

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [allPasses removeObjectAtIndex:indexPath.row]; 
     // Delete the row from the data source 
     PassWhizEntity *passToRemove = self.allPasses[indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     // Get the local context 
     NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread]; 
     [passToRemove MR_deleteInContext:localContext]; 
     [localContext MR_save]; 


    } 

이 잘 작동하고 당신에게 당신이 어떤 행을 삭제할 수 있지만 때 마지막 행을 응용 프로그램 충돌을 삭제하고 이 오류가 발생합니다.

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 

그러나 어떻게 수정해야할지 모르겠다. 나는 그것의 아주 간단한 수정을 확신하지만 배열에 하나를 추가하려고 시도하고 나는 무엇을 해야할지 잘 모르겠습니다. 어떤 아이디어라도 감사 할 것입니다!

+1

테이블에 남아있는 마지막 행을 삭제 하시겠습니까? @Wain이 올 바르면 이미 제거한 배열의 내용에 액세스하려고합니다. 사용자가 마지막 행을 지우지 않으려면'canEditRowAtIndexPath'를 사용하여 보관하려는 행을 비활성화하십시오. –

답변

2

이 라인이 잘못된 순서로되어 있기 때문에이 제대로 작동하지 않습니다 : 마지막 항목에 도달 할 때까지

[allPasses removeObjectAtIndex:indexPath.row]; 
// Delete the row from the data source 
PassWhizEntity *passToRemove = self.allPasses[indexPath.row]; 

당신이 순간에있는 모든 숨겨진 문제입니다.

항목을 배열에서 제거하기 전에 삭제할 수 있도록 해당 줄의 순서를 반대로 변경하십시오.

관련 문제