滑动以删除单元格不会取消UIButton操作

时间:2014-05-16 19:22:34

标签: ios uitableview uibutton swipe-gesture

我的UITableView启用了滑动删除功能。每个单元格上都有UIButton执行操作(在这种情况下,执行segue)。

我希望如果我通过触摸按钮来滑动单元格,则会取消/忽略按钮的操作,并且仅处理滑动。然而,实际发生的是检测和处理两种手势(轻扫+轻击)。

这意味着如果我只是想删除一个单元格并且意外地删除了#34;通过触摸按钮滑动,应用程序将转到下一个屏幕。

在这种情况下,如何强制我的应用忽略点按?

4 个答案:

答案 0 :(得分:9)

对于我来说,八月的回答非常好,但我想出了如何让它变得更好:

检查表是否处于编辑模式以确定按钮是否应该执行其操作将使其按预期运行,但用户体验仍会存在问题:

如果用户想要退出编辑模式,他应该能够点击单元格中的任何位置来实现该功能,包括按钮。但是,应用程序仍会首先分析UIButton的操作,点按该按钮不会退出编辑模式。

我找到的解决方案是在进入编辑模式时禁用按钮的用户互动,并在完成后重新启用它:

// View with tag = 1 is the UIButton in question
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
  [(UIButton *)[[tableView cellForRowAtIndexPath:indexPath] viewWithTag:1] setUserInteractionEnabled:NO];
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
  [(UIButton *)[[tableView cellForRowAtIndexPath:indexPath] viewWithTag:1] setUserInteractionEnabled:YES];
}

这样,拖动按钮进入编辑模式不会触发按钮的动作,而将其按到退出编辑模式的确会退出编辑模式。

答案 1 :(得分:2)

只要单元格已进入编辑模式,一种优雅的方法是忽略按钮点击。这是有效的,因为滑动删除手势将导致在调用按钮点击操作之前调用willBeginEditingRowAtIndexPath。

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.isEditing = YES;
}

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.isEditing = NO;
}

// button tapped
- (IBAction)tap:(id)sender
{
    if (self.isEditing) {
        NSLog(@"Ignore it");
    }
    else {
        NSLog(@"Tap");
        // perform segue
    }
}

答案 2 :(得分:2)

也可以在你的手机的子类中做到这一点:

override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    actionButton?.userInteractionEnabled = !editing
}

答案 3 :(得分:0)

因为从按下按钮调用我的函数是我的主@IBAction func delayButton(_ sender: Any) { noDelayLabel.isHidden = false DispatchQueue.main.asyncAfter( deadline: DispatchTime.now() + \*delay value here*\}, execute: { self.delayLabel.isHidden = false } ) } 类的代理并且作为IBAction连接到UITableViewController,我的UITableViewCell中的按钮仍然是在滑动时按下UITableViewCell按下滑动。

为了阻止我使用相同的UIButton代理作为接受的答案,但必须设置文件级变量来监控是否正在进行编辑。

UITableView
相关问题