2011-07-14 10 views
0

を、Iは、tableArraysクラス変数(NSMutableArraysのNSMutableArrayのあることが、以下の方法のUITableViewからセルを削除する - 各セクションの1 NSMutableArrayのを表す - 私のiPhoneアプリでエラー

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [tableArrays count]; 
} 

注とのUITableViewを有します私のテーブルビュー)。さて、のUITableViewは編集をサポートし、私は次のような方法ので

- (void)tableView:(UITableView *)theTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    ...some code... 

    NSLog(@"tableArrays count is %i",[tableArrays count]); 
    [tableArrays removeObjectAtIndex:indexPath.section]; 
    NSLog(@"tableArrays is now %@",tableArrays); 
    NSLog(@"tableArrays count is now %i",[tableArrays count]); 


    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

} 

を持っていることを、私は、アプリを実行し、私は私のテーブルビューに2つのセクションがあります。行の削除は、セクション内の最後の行を削除する場合を除いて正常に機能します。私は、セクション1の最後の行を削除すると、私は次の出力を参照して、上記のコードに従って:

tableArrays count is 2 
tableArrays is now (("Blah1","Blah2")) 
tableArrays count is now 1 

だから、はっきりと、私のtableArraysカウントが1ずつ減少が、私は次のエラーを取得する:

...The number of sections contained in the tableView after the update (1) must be qual to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or dleted (0 inserted, 0 deleted).' 

答えて

1

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;では、オブジェクトの総数が返されているとします.1つのセクションのみを表示したいとします。

このメソッドで1つ返すようにしてください。

また[tableArrays count]-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;に返す必要があります。

このようにうまくいくはずです。また、section

にあります。

[tableArrays removeObjectAtIndex:section];とは何ですか? indexPath.sectionを意味しましたか?

コメントをいただきありがとうございます。セクション番号を返すメソッドは正しいですが、セルを削除するにはそのようにする必要があります。

NSLog(@"tableArrays count is %i",[tableArrays count]); 
NSMutableArray *sectionArray = [tableArrays objectAtIndex:indexPath.section]; 
[sectionArray removeObjectAtIndex:indexPath.row]; 
//[tableArrays removeObjectAtIndex:indexPath.section]; 
NSLog(@"tableArrays is now %@",tableArrays); 
NSLog(@"tableArrays count is now %i",[tableArrays count]); 
+0

改訂をご覧ください。 NSMutableArraysのNSMutableArray(tableViewの各セクションの1つのNSMutableArrayを表します) – CodeGuy

+0

が私の答えを更新しました –

+0

これを解決しましたか? –

1

キーを使用すると、セクションの最後の行を削除しようとしているとき

[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 

を使用することです。

関連する問題