UITableView不会进入编辑模式,也不会编辑!

时间:2011-04-27 11:39:10

标签: iphone objective-c uitableview

我很难开展一个新项目。主结构是RootViewController中的tableView,其中一些行显示用户的实际状态和一些信息。我现在正在处理无法正常工作的编辑模式。

在导航栏中有一个调用edit的按钮,这里是代码:

- (void)edit {
    NSLog(@"Edit pressed");
    if (edit) {
        NSLog(@"Deactivate edit\n");
        edit = NO;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Bearbeiten" style:UIBarButtonItemStyleBordered target:self action:@selector(edit)];
        [contentTableView setEditing:NO animated:YES];
    } else {
        NSLog(@"Activate edit\n");
        edit = YES;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Fertig" style:UIBarButtonItemStyleDone target:self action:@selector(edit)];
        [contentTableView setEditing:YES animated:YES];
    }
}

我的viewController是UITableViewController的子类,contentTableView不是nil ...我实现了以下方法:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSMutableDictionary *oldDict = [[NSMutableDictionary alloc] initWithDictionary:[saver read]];
    NSDictionary *movingDict = [[oldDict objectForKey:@"content"] objectAtIndex:sourceIndexPath.row];
    [[oldDict objectForKey:@"content"] removeObjectAtIndex:sourceIndexPath.row];
    [[oldDict objectForKey:@"content"] insertObject:movingDict atIndex:destinationIndexPath.row];
    [saver write:oldDict];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSMutableDictionary *oldDict = [[NSMutableDictionary alloc] initWithDictionary:[saver read]];
    [[oldDict objectForKey:@"content"] removeObjectAtIndex:indexPath.row];
    [saver write:oldDict];

    [contentTableView beginUpdates];
    [contentTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //This doesn't work!
    [contentTableView endUpdates];

}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"Entfernen";
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

我从另一个项目中复制了这个并更改了部分代码,但是我没有触及的主要内容!

会发生什么?

当我按下编辑('Bearbeiten')时,会调用“编辑”功能。它用Done('Fertig')替换按钮并写入正确的日志:

> 2011-04-27 13:35:44.338 MyApp[1113:207] Edit pressed
> 2011-04-27 13:35:44.339 MyApp[1113:207] Activate edit

如果我在单元格上滑动,则会出现“删除”(“Entfernen”)按钮,当我按下它时,该按钮会消失,但该行仍然存在,但它已从内容中删除。

有谁知道我做错了什么或忘记了什么?

提前致谢,mavrick3。

修改1:

我忘了说:方法[tableView reloadData];不会调用numberOfRowsInSection ...

1 个答案:

答案 0 :(得分:0)

这些问题似乎是运行iOS 4.3.2的iOS模拟器中的一个错误。

如果您发现此问题,请将部署目标设置为较旧的SDK,例如4.2并在其上运行您的应用程序。

相关问题