2012-01-28 12 views
1

で最後のセルを削除これは私のUITableViewでセルを削除するための私のコードです:はUITableView-セクション

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView beginUpdates]; 

    if([tableView numberOfRowsInSection:[indexPath section]] > 1) { 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
      withRowAnimation:UITableViewRowAnimationFade]; 
    } else { 
     [tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] 
      withRowAnimation:UITableViewRowAnimationFade]; 
    } 

    [tableView endUpdates]; 
} 

あり、セルが削除される前に一部のデータが更新されているが、私はそれはかなり確信していますデータソースの配列のcountは、セルが削除されるたびに常に1つ少ないため、正しく更新されます。これは予想される動作です。しかし、何らかの理由で、セクションの最後のセルを削除するたびに、私は'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

インターネットで見つけることができたので、私はこれを正しくやっているので、非常にイライラしています。多分私は睡眠が必要です、それはしばらくありました。ここにどんなアイデア?

ソリューション:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { 
    if(tableIsEmpty) { 
     //**this** line was the culprit- returning zero here fixed the problem 
     return 0; 
    } else if(section == ([tableView numberOfSections] - 1)) { 
     //this is the last section- it only needs one row for the 'Delete all' button 
     return 1; 
    } else { 
     //figure out how many rows are needed for the section 
    } 
} 

答えて

4

あなたの問題は、あなたのnumberOfRowsInSection:メソッドの実装です。あなたの削除はうまくいくようです。行を削除したら、numberOfRowsInSection:メソッドの行数を返すために使用したソースを更新していないと思います。そこに不一致があるため、エラーが発生します。そのコードを共有してください。

+0

はい、はい!あなたはまさに正しいのです。私が必要としていたのは、私の問題*が私の削除コードではないことを私に伝える人だったと思う。私はそれをまっすぐにしました - 私はいつも "Delete all"セルを持つ1つの余分なセクションをどのように表示していたのでしょうか?最後のセクションが削除されたときにそれを消さずにいました。要するに、たくさんの手伝いをしたので、ありがとう! –

+0

@Rickayヘルプメイトになってうれしいです。 – MadhavanRP

関連する問題