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 个部分,第一个(从 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

6

努力让这个工作。这是我在 tableView 中添加一行的代码:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView beginUpdates];
[dataSource insertObject:[artistField text] atIndex:0];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
于 2009-05-27T02:14:48.140 回答
1

我似乎记得 numberOfRowsInSection: 会在您调用 deleteRows 或 insertRow 时被调用,您需要非常小心实际的 numberOfRowsInSection cliams 是否与您的更改相匹配。在这种情况下,您可能想尝试移动 iSelectedSection = [indexPath section]; 行到 endUpdates 之后。

于 2008-12-02T04:25:00.737 回答
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 节添加五个新行。由于每个节已经有第 0 行,这将将第 2 节第 0 行的第二个实例添加到表中。

我建议让你的循环从 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 回答