【问题标题】:iPhone, hook for edit/done button click in table viewiPhone,在表格视图中单击编辑/完成按钮的钩子
【发布时间】:2009-07-01 19:59:31
【问题描述】:

在我的表格视图控制器中有

self.navigationItem.leftBarButtonItem = self.editButtonItem;

在左上角生成一个常规的编辑/完成按钮。因此,一旦用户单击“编辑”,按钮标题更改为“完成”,表格条目可能会被删除或重新排序。一旦用户实际单击“完成”,我想得到通知。有钩子吗?

背景:我想保留条目的顺序,即下次用户打开此视图时,我想以最近最少使用的顺序显示条目。

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    对于那些仍然对此问题感兴趣的人(或回答:P)

    UITableView API

    透露有一个- (void)setEditing:(BOOL)editing animated:(BOOL)animate方法 每次按下此编辑/完成按钮时都会调用这些方法。您必须简单地通过 (BOOL)editing 参数检查使用了哪个参数。最后但同样重要的是,您必须从最初的编辑/完成按钮调用正确的方法。

    只需将此方法添加到您的 uitableview 类中

    - (void)setEditing:(BOOL)editing animated:(BOOL)animate
    {
        [super setEditing:editing animated:animate];
        if(editing)
        {
            NSLog(@"editMode on");
        }
        else
        {
            NSLog(@"Done leave editmode");
        }
    }
    

    【讨论】:

    • 请记住,如果任何以编程方式将视图置于编辑模式,也会调用 setEditing:animated()。我添加了一个仅调用 [super setEditing] 的附加方法,我可以使用该方法从外部设置编辑模式。
    • 注意:如果您通过UIViewController 作为UITableView 代表,请务必告诉UITableView 设置表的 [self.tableView setEditing:editing animated:animated] 的编辑状态。
    【解决方案2】:

    对于那些不想覆盖 UITableView(例如,如果您正在使用 UITableViewController)的人,这是我使用的一个简单而干净的解决方案。它基本上涉及创建您自己的编辑按钮项并使用 tableView 的editing 标志来跟踪编辑与完成。锦上添花的是,当表格为空时,它会显示一个“+”按钮(而不是“编辑”)以添加新项目。

    - (void) updateEditButtonVisibility
    {
        // tableItems represents the data structure that s
        if ([tableItems count] > 0)
        {
            UIBarButtonSystemItem editButtonType = self.tableView.editing ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit;
            UIBarButtonItem *editButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:editButtonType
                                                                                            target:self
                                                                                            action:@selector(editButtonTouched)];
    
            self.navigationItem.rightBarButtonItem = editButtonItem;
            [editButtonItem release];
        }
        else
        {
            UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                           target:self
                                                                                           action:@selector(addButtonTouched)];
            self.navigationItem.rightBarButtonItem = addButtonItem;
            [addButtonItem release];
        }
    }
    
    - (void) editButtonTouched
    {
        // edit/done button has been touched
    
        [self.tableView setEditing:!self.tableView.editing animated:YES];
        [self updateEditButtonVisibility];
    }
    
    - (void) addButtonTouched
    {
        // logic to allow user to add new items goes here
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [self updateEditButtonVisibility];
    }
    

    【讨论】:

      【解决方案3】:

      这是在按下条形按钮时获得通知的标准方式:

      self.editButtonItem.target = self;
      self.editButtonItem.action = @selector(buttonPushed:);
      
      ...
      
      - (void) buttonPushed:(id)sender
      {
      // do stuff here
      }
      

      【讨论】:

      • 嗯,我知道。关键是我不想干扰按钮已经做的事情(修改列表条目、切换其标题等)。我只是想知道何时在“完成”状态下单击它。
      • UIBarButtonItems 不是从 UIControl 派生的,因此您不能只向它添加另一个目标。您始终可以自己捕获操作并维护状态。这并不难。或者,获取 buttonPushed 调用(上图),然后设置一个“忽略”标志并转身并在按钮上合成一个一次性事件,让它完成它的工作。在此处合成触摸事件的详细信息:cocoawithlove.com/2008/10/…
      【解决方案4】:

      可以更改操作。单击编辑按钮后,它会显示删除按钮,而是可以显示拒绝/验证/修改按钮。并更改相应的操作而不是删除选项

      谢谢 思维导图

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-16
        • 1970-01-01
        相关资源
        最近更新 更多