2016-07-07 4 views
0

私はObjective-Cを初めて使いました。だから私はセクション(5セル)の5行でテーブルビューを持って、私はそれらを削除することができますが、削除ボタンをクリックすると、アプリケーションがクラッシュし続けます。クラッシュする理由は、[self.myListItems count]を返す代わりにセクションに5行があるからです。セクション内に5行を保持しているときに、このコードを以下のように修正するにはどうすればよいですか?テーブルビューのセルiOSを削除する方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 


     [self.myListItems removeObjectAtIndex:indexPath.row]; 
     [self saveList]; 

     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

エラーがある:中アサーションの失敗 - [のUITableView _endCellAnimationsWithContext:]

+0

あなたはテーブルビューをリロードしましたか?そしてbtwなぜあなたは行を減らす必要があります、セルを削除する場合でも5行を保持したいですか? – DCDC

+0

'numberOfRowsInSection'に静的5を返していることを意味していますか? –

+0

http://stackoverflow.com/questions/10134841/assertion-failure-in-uitableview-endcellanimationswithcontext –

答えて

0

は、これらを使用してみてください:

のviewDidLoadで
[self.myListItems removeObjectAtIndex:indexPath.row]; 

[tableView beginUpdate]; 
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[tableView endUpdate]; 

[self saveList]; 
3

self.tableView.allowsMultipleSelectionDuringEditing = NO; 

そして

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return YES if you want the specified item to be editable. 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //add code here for when you hit delete 
     [self.myListItems removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self saveList]; 
    }  
} 

注意 - セクション内の最後のアイテムである行を削除する場合は、セクション全体を削除する必要があります(そうしないと、セクション数が正しくない場合があります)。

これが役に立ちます。セルが削除された後

0

変更次の行

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (editingStyle == UITableViewCellEditingStyleDelete) { 
// Delete the row from the data source 
[tableView beginUpdates] 
[self.myListItems removeObjectAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[tableView endUpdate]; 
[self saveList]; 
} 
関連する問題