の行を削除しようとすると、奇妙な動作が発生しUITableView
ます。
アプリに 2 つのテーブルビューがあり、どちらもtableView:commitEditingStyle:forRowAtIndexPath
: メソッドに実質的に同じコードがあります。1 つは問題なく動作していますが、もう 1 つはこの問題を抱えています。たとえば、テーブル ビューに X 行以上あるとしましょう。セルの再利用とスクロールの両方が有効になっています。リストの上部にある行を削除すると、画面の下部に表示されているセルは、削除ボタンを押す前に下部のセルによってすべて複製されます。たとえば、5 行を削除すると、テーブルビューに同一のセルが表示されます。次に、テーブルビューをスクロールすると、すべて正常に戻ります。(または、viewcontrollerを変更してからtableviewのものに戻ると)
作業テーブルビューのコードは次のとおりです。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (search.text.length == 0) {
[tableArray removeObjectAtIndex:indexPath.row];
[goalTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self setBG];
tempArray = [NSArray arrayWithArray:tableArray];
[[NSUserDefaults standardUserDefaults]setObject:tempArray forKey:@"currentArray"];
totalGoals = [[NSUserDefaults standardUserDefaults] integerForKey:@"totalGoals"];
totalGoals = totalGoals - 1;
[[NSUserDefaults standardUserDefaults] setInteger:totalGoals forKey:@"totalGoals"];
[[NSUserDefaults standardUserDefaults] synchronize];
} } }
そして、ここに欠陥のあるテーブルビューのコードがあります:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (search.text.length == 0) {
[tableArray removeObjectAtIndex:indexPath.row];
[goalTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self setBG];
tempArray = [NSArray arrayWithArray:tableArray];
[[NSUserDefaults standardUserDefaults]setObject:tempArray forKey:@"completedArray"];
completedGoals = [[NSUserDefaults standardUserDefaults] integerForKey:@"completedGoals"];
completedGoals = completedGoals - 1;
[[NSUserDefaults standardUserDefaults] setInteger:completedGoals forKey:@"completedGoals"];
[[NSUserDefaults standardUserDefaults] synchronize];
} } }
まあ、それはほとんど同じなので、私はここで少し迷っています - 彼らは同じコードを持っていて、一方は正常に動作し、もう一方はそうではありません. それはcellForRowAtIndexPath:
方法の問題でしょうか?疑問に思っていますが、この方法では何も見つかりませんでした。さらに、それらも実質的に同じです。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * identifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
//here I'm setting up the cells, labels, and stuff.
}
if (search.text.length == 0 && [filteredArray count] == 0) {
NSDictionary * dict = [tableArray objectAtIndex:[tableArray count] - 1 - indexPath.row];
//here I use the values on the dict to do some stuff with labels, etc.
}
return cell;
}
問題は、次の行で配列を「反転」していることです。
NSDictionary * dict = [tableArray objectAtIndex:[tableArray count] - 1 - indexPath.row];
そうしないと、つまり、次のようなことを[tableArray objectAtIndex:indexPath.row]
すると、削除の問題はまったく発生しません。逆配列が原因で起こるものです。ああ、私がセルにデータを入力しているとき(たとえば、ビューがロードされているとき)、セルの再利用は問題なく機能しており、セルの重複などはありません。行を削除するときにのみ発生するものです。