7

同じ UITableView から UITableViewCells を挿入したり削除したりするのにかなり苦労しています!

私は通常、コードを投稿しませんが、これが問題を抱えている場所を示す最良の方法だと思いました:


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}


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

    if (iSelectedSection == section) return 5;
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //NSLog(@"drawing row:%d section:%d", [indexPath row], [indexPath section]);

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    if (iSelectedSection == [indexPath section]) {
        cell.textColor = [UIColor redColor];
    } else {
        cell.textColor = [UIColor blackColor];
    }   


    cell.text = [NSString stringWithFormat:@"Section: %d Row: %d", [indexPath section], [indexPath row]];

    // Set up the cell
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic -- create and push a new view controller

    if ([indexPath row] == 0) {

        NSMutableArray *rowsToRemove = [NSMutableArray array];
        NSMutableArray *rowsToAdd = [NSMutableArray array];

        for(int i=0; i<5; i++) {

            //NSLog(@"Adding row:%d section:%d ", i, [indexPath section]);
            //NSLog(@"Removing row:%d section:%d ", i, iSelectedSection);

            [rowsToAdd addObject:[NSIndexPath indexPathForRow:i inSection:[indexPath section]]];
            [rowsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:iSelectedSection]];

        }

        iSelectedSection = [indexPath section];

        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:rowsToRemove withRowAnimation:YES];
        [tableView insertRowsAtIndexPaths:rowsToAdd withRowAnimation:YES];
        [tableView endUpdates];

    }
}

このコードは 5 つのセクションを作成し、1 番目 (0 からインデックス付け) は 5 行で構成されます。セクションを選択すると、以前に選択したセクションから行が削除され、選択したセクションに行が追加されます。

図的に、アプリをロードすると、次のようになります。

http://www.freeimagehosting.net/uploads/1b9f2d57e7.png http://www.freeimagehosting.net/uploads/1b9f2d57e7.png

画像はこちら: http://www.freeimagehosting.net/uploads/1b9f2d57e7.png

セクション 2 のテーブル行 0 を選択した後、セクション 1 (デフォルトで選択されています) の行を削除し、セクション 2 の行を追加します。しかし、次のようになります。

http://www.freeimagehosting.net/uploads/6d5d904e84.png http://www.freeimagehosting.net/uploads/6d5d904e84.png

画像はこちら: http://www.freeimagehosting.net/uploads/6d5d904e84.png

...これは私が期待するものではありません! セクション 2 の最初の行は、確実に削除されますが、何とか残っているようです。

[tableView reloadData]を実行すると、すべてが正常に表示されます...しかし、私は明らかに素晴らしいアニメーションを忘れています。

誰かがここに光を当てることができれば、本当に感謝しています!それは私を少し夢中にさせています!

ありがとう、ニック。

4

5 に答える 5

1

numberOfRowsInSection: は、deleteRows または insertRow を呼び出すときに呼び出されることを覚えているようです。実際の numberOfRowsInSection クライアムが変更と一致するように注意する必要があります。この場合、iSelectedSection = [indexPath section]; を移動してみてください。endUpdates の後までの行。

于 2008-12-02T04:25:00.737 に答える
1

これをどこで読んだか覚えていませんが、テーブル ビュー デリゲート関数の 1 つからテーブル行の更新 (挿入と削除) を実行するべきではないと思います。より良い代替手段は、更新をオブジェクトとして実行するために必要な情報を performSelectorOnMainThread に渡すことだと思います。何かのようなもの:

- (void)tableView:(UITableView *)tableView 
  didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // ....
    [self performSelectorOnMainThread: @selector(insertRows:)
                           withObject: someObjectOrNil]; // double check args
}

- (void) insertRows: (NSObject*)someObjectOrNil {
    [tableView beginUpdates];
    // update logic
    [tableView endUpdates];

    // don't call reloadData here, but ensure that data returned from the 
    // table view delegate functions are in sync
}
于 2008-12-08T22:18:15.400 に答える
0

投稿したコードでは、ループ インデックスは 0 から 4 まで実行されます。これは、セクション 1 のすべての行を削除し、セクション 2 に 5 つの新しい行を追加することを示唆しています。各セクションには既に行 0 があるため、これはセクション 2、行 0 の 2番目のインスタンスをテーブルに追加します。

ループを 1 から 4 まで実行することをお勧めします。

for (int i=1; i<5; i++)
{
    // ...
}
于 2008-12-01T20:31:27.977 に答える
0

参考までに、このバグは 2.2 iPhone アップデートで完全に修正されたようです。ありがとうアップル!ニック。

于 2008-12-30T10:42:45.103 に答える