2010-02-11 6 views
1

DeleteRows 코드를 누를 때마다 업데이트 전후의 행 수가 동일해야한다는 예외가 발생합니다.TableView에서 행을 삭제할 때 예외가 발생했습니다.

이유 : 잘못된 업데이트 : 섹션 0의 행 개수가 잘못되었습니다. 업데이트 (3) 후 기존 섹션에 포함 된 행 수는 이전에 해당 섹션에 포함 된 행 수와 같아야합니다. 업데이트 (3), 해당 섹션에서 삽입되거나 삭제 된 행 수를 더하거나 뺍니다 (삽입 된 0, 삭제 된 1).

내 코드는 다음과 같습니다

 public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath) 
    { 
     if (editingStyle == UITableViewCellEditingStyle.Delete) 
     { 
      tableView.DeleteRows(new [] { indexPath }, UITableViewRowAnimation.Fade); 
    // Remove the step from the set of calculations 
    _calculation.Steps.RemoveAt(indexPath.Row); 
     } 
    } 

답변

1

당신은 아마 번호가 삭제 이전보다 하나 낮은하기 위해 indexPath.section에 대한

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 

반환 변경해야합니다.

으로는 여기에 대답 : 나는 나를 위해 일한 무엇을 발견 Delete row from uitableview crashes

+0

잘못된 순서로 내 DeleteRows 및 RemoveAt을하고 있다고 말한 것으로부터 깨달을 수있었습니다. 일단 내가 주문을 변경하면 괜찮 았어. 감사! –

0

이 tableView.DeleteRows을 제거하는 것이었다 (새 [] {indexPath}, UITableViewRowAnimation.Fade); 방법은 다음과 같아야합니다.

public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath) 
    { 
     if (editingStyle == UITableViewCellEditingStyle.Delete) 
     { 
      // Remove the step from the set of calculations 
      _calculation.Steps.RemoveAt(indexPath.Row); 
      tableView.reloadData(); 
     } 
    } 
관련 문제