确定在UITableViewController中编辑操作是删除还是添加

时间:2011-09-10 10:34:19

标签: iphone objective-c ios cocoa-touch

所以我希望能够确定如何区分插入操作和删除操作,以便我可以做出相应的响应。目前我有这个代码来创建“完成”,“编辑”和“添加”按钮

- (void)initializeNavigationBarButtons
{
    UIBarButtonItem *newEditButton = 
    [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemEdit 
     target:self action:@selector(performEdit:)];

    self.editButton = newEditButton;
    [newEditButton release];

    self.navigationItem.rightBarButtonItem = self.editButton;

    UIBarButtonItem *newDoneButton = 
    [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
     target:self action:@selector(performDone:)];

    self.doneButton = newDoneButton;
    [newDoneButton release];

    UIBarButtonItem *newAddButton = 
    [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
     target:self action:@selector(performAdd:)];

    self.addButton = newAddButton;
    [newAddButton release];
    self.navigationItem.leftBarButtonItem = self.addButton;

}

然后我将这3个作为按钮的所谓“回调”函数:

- (void)performDone:(id)paramSender
{
    [self.tableView setEditing:NO animated:YES];


    [self.navigationItem setRightBarButtonItem:self.editButton
                                      animated:YES];

    [self.navigationItem setLeftBarButtonItem:self.addButton
                                      animated:YES];
}

- (void)performEdit:(id)paramSender
{
    NSLog(@"Callback Called");
    [self.tableView setEditing:YES animated:YES];

    [self.navigationItem setRightBarButtonItem:self.doneButton
                                      animated:YES];

    [self.navigationItem setLeftBarButtonItem:self.doneButton
                                     animated:YES];
}

- (void)performAdd:(id)paramSender
{
    [self.tableView setEditing:YES animated:YES];

    [self.navigationItem setRightBarButtonItem:self.doneButton
                                      animated:YES];

    [self.navigationItem setLeftBarButtonItem:self.doneButton
                                      animated:YES];
}

这里是我“应该”确定它是添加还是删除操作的地方:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *output = (isDeleting) ? @"Deleting" : @"Adding";

    NSLog(@"%@", output);

    UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;

    if ([tableView isEqual:self.tableView]){
        if (self.isDeleting == YES){
            result = UITableViewCellEditingStyleDelete;
        }
        else{
            result = UITableViewCellEditingStyleInsert;
        }
    }

    return result;
}

然而,我不知道我应该在哪里设置self.isDeleting和self.isAdding。我试图在回调中设置它们,但似乎首先调用tableView:cellEditingStyleForRowAtIndexPath:在我的viewDidLoad中,它们的默认值为NO。

那么如何才能正确设置isAdding和isDeleting的值,以便能够在tableView:cellEditingStyleForRowAtIndexPath:方法中进行相应的操作?

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果导航栏中有“添加”按钮,为什么不按下按钮时执行“添加”操作,而不是使tableview可编辑?当单元格具有内容时,将单元格的编辑样式设置为UITableViewCellEditingStyleInsert也没什么意义。通常的流程是在表格外部有一个“添加”按钮(例如:在导航栏中)执行添加操作,或者作为编辑风格时在tableview中的最后一个(或第一个)单元格,以及按下单元格,执行添加操作。

因此,您可以在导航栏中保留“添加”按钮,并使用UITableViewCellEditingStyleDelete使所有单元格均可编辑,或仅保留“编辑”/“完成”按钮,并在编辑时添加新单元格(同时tableview是可编辑的,可以使用UITableViewCellEditingStyleInsert进行编辑。

旁注:代替if ([tableView isEqual:self.tableView]),您应该更好地使用if (tableView == self.tableView),因为您要检查它是否是同一个实例,而不是它们是否相等。