2011-11-01 5 views
1

셀을 삭제하고 삭제할 때 셀을 편집 모드로 전환하고 왼쪽에 빨간색 화살표가 표시되면 UITableViewController가 있습니다. 그러나 데이터는 이미 삭제되었습니다. 앱을 다시 시작하면 사라집니다. 내 코드의 내용이 의심스러워 보이나요?셀을 삭제하려고 할 때 UITableView가 작동합니다.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

- (void)setEditing:(BOOL)editing animated:(BOOL)animate 
{ 
    [self.tableView setEditing: !self.tableView.editing animated:YES]; 

    if (self.tableView.editing) 
     [self.navigationItem.leftBarButtonItem setTitle:@"Done"]; 
    else 
     [self.navigationItem.leftBarButtonItem setTitle:@"Edit"]; 
} 

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     PFObject *routine= [self.routineArray objectAtIndex:indexPath.row]; 
     [routine deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
      if (!error) { 
       [self.tableView reloadData]; 
      } else { 
       // There was an error saving the gameScore. 
      } 
     }]; 
    } 
} 

답변

1

그런 다음, 당신은 dataSource self.routineArray에서 항목을 제거해야합니다 -deleteRowsAtIndexPaths:withRowAnimation:를 사용하여있는 tableView에서 삭제, 당신의 코어 데이터 저장소에서 제거합니다. 당신의 dataSource에 여전히 존재하는 tableView를 삭제할 수 없기 때문에 순서가 중요합니다. 그렇지 않으면 tableView가 dataSource의 항목 수와 동기화되지 않아서 예외가 발생합니다.

이 예에서는 먼저 Data Store에서 삭제 된 다음 dataSource에서 삭제 된 다음 tableView에서 삭제됩니다.

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     PFObject *routine= [self.routineArray objectAtIndex:indexPath.row]; 
     [routine deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
      if (!error) { 
       [self.routineArray removeObjectAtIndex:indexPath.row]; 
       [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      } else { 
       // There was an error saving the gameScore. 
      } 
     }]; 
    } 
} 
+0

안녕하세요, 그래도 작동하지만 애니메이션은 아직 설정되어 있습니다. 스 와이프 한 다음 삭제를 누르면 왼쪽에 마이너스 기호가 표시되고 마지막으로 삭제됩니다. –

+0

이것은 내가 주문한 것이 중요하다는 것을 의미합니다. '-tableView : numberOfRowsInSection :'에 대해 무엇을 반환하겠습니까? 해당 dataSource 메소드에서 리턴되는 숫자가 테이블보기의 행 수와 동일한 지 확인해야합니다. tableView가이 메소드를 호출하고 번호가 다른 경우 언제든지 NSInternalInconsistencyException을 발생시킵니다. 혹시 NSFetchedResultsController를 사용하여 테이블 뷰를 관리하고 있습니까? 'self.routineArray'에서 객체를 삭제하고 tableView에서 먼저 행을 제거하고'[tableView reloadData]'를 실행하지 않도록하십시오. – Andrew

+0

더 이상 충돌이 발생하지 않지만 애니메이션이 엉망입니다. 나는 당신이 볼 수 있으면 비디오로 여기에 새로운 질문을했습니다. 감사. http://stackoverflow.com/questions/7976249/stange-animation-when-deleting-uitableviewcell –

관련 문제