iphone - 判断UITableViewController中的编辑操作是删除还是添加

标签 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: 方法中采取相应的操作?

提前致谢!

最佳答案

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

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

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

关于iphone - 判断UITableViewController中的编辑操作是删除还是添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7371117/

相关文章:

ios - DismissViewController 无法访问presentingViewController

objective-c - 我应该保留 NSCalendar 对象以供持久使用吗?

ios - 使用代码模拟 UIButton Click 事件

iphone - 以编程方式设置 ipad 应用程序背景?

ios - UITableView titleForHeaderInSection 未返回正确的 stringWithFormat

objective-c - 在 Objective-C 中,未赋值的 int 变量是 nil 吗?

ios - OpenGL格式纹理

iphone - 如何从凹凸中使用多个 block 进行 NSKeyedUnarchiver ?

iphone - 如何在不使用选项卡栏的情况下为 iPhone 应用程序创建选项卡?

ios - 在 Xcode 中通过 IB 选择自定义字体