0

我正在使用 SplitViewController 和核心数据。我的主视图是一个简单的 TableView,就像 SplitViewController 模板一样。我有这三种方法:

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



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

        [self saveContext];
    }   
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

我认为当我滑动行删除它时,这足以显示删除按钮。现在发生的情况是,如果您第一次并且仅第一次滑动它,删除按钮会出现然后消失。如果我再次滑动,什么也不会发生。我需要重新启动模拟器才能让它再次显示。在我的 TableViewCell 上,我有一个 UILabel 和一个 UITextField。我想也许 UITextField 会拦截滑动,但我调整了它们的大小,所以它们只在左半边以防万一。

编辑:

所以我仍然没有弄清楚为什么会发生这种情况。我想我会添加一个编辑按钮来测试 tableView setEditing: 是否被调用。所以在我的行动方法中,我这样做:

- (IBAction)setEditMode:(id)sender {
    [self.tableView setEditing:YES animated:YES];
}

这个方法确实被调用了,但是 tableView 永远不会进入编辑模式。所有这些都在 UITableViewController 的子类中。还有什么我想念的吗?谢谢!

4

1 回答 1

0

尝试这个,

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

    if(editingStyle == UITableViewCellEditingStyleDelete) {

        //Get the object to delete from the array.
        Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];
        [appDelegate removeCoffee:coffeeObj];

        //Delete the object from the table.
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
于 2012-09-28T04:29:40.133 回答