父控制器阻止子表视图UITableViewRowAction swift

时间:2017-08-16 09:08:45

标签: ios swift uitableview

我正在使用库调用页面菜单(https://github.com/PageMenu/PageMenu),它允许我在视图控制器中添加子视图控制器。我可以在这个视图控制器之间滚动。在其中一个子视图控制器中我使用UITableView.i使用UITableViewRowAction来删除单元格。不知道为什么,但桌子没有检测到轻扫手势。如果我禁用页面菜单滚动然后表视图UITableViewRowAction工作。我在谷歌搜索了很多关于这个。我没有找到任何解决方案。谢谢

attached the screenshot of my app

1 个答案:

答案 0 :(得分:3)

在使用该库后,我发现该问题与在CAPSPageMenu视图控制器中禁用滚动视图手势识别器有关。

按照以下步骤在其中一个子视图控制器中启用表视图编辑。

UIScrollView目标<}展开PageMenu这样的代码:

class CustomScrollView : UIScrollView, UIGestureRecognizerDelegate {

  override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    gestureRecognizer.cancelsTouchesInView = false
    return true
  }

  func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return ((self.delegate as! CAPSPageMenu).gestureDelegate?.gestureRecognizerShouldRecognizeSimultaneouslyWith(gestureRecognizer, otherGestureRecognizer: otherGestureRecognizer))!
  }
}

然后在CAPSPageMenu中,替换:

let controllerScrollView = UIScrollView()

let controllerScrollView = CustomScrollView()

在同一文件中声明协议:

@objc public protocol CustomGestureDelegate {
  func gestureRecognizerShouldRecognizeSimultaneouslyWith(_ gestureRecognizer: UIGestureRecognizer, otherGestureRecognizer: UIGestureRecognizer) -> Bool
}

并将此属性添加到CAPSPageMenu

open weak var gestureDelegate: CustomGestureDelegate?

我在controllerArray中将ViewController作为属性,因此我们可以稍后使用表视图过滤出视图控制器。

var controllerArray : [UIViewController] = []

然后让ViewControllerCustomGestureDelegate符合ViewController: UIViewController, CustomGestureDelegate,并在viewDidLoad中设置:

pageMenu!.gestureDelegate = self

ViewController(您的容器视图控制器)中实现此功能:

func gestureRecognizerShouldRecognizeSimultaneouslyWith(_ gestureRecognizer: UIGestureRecognizer, otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  if pageMenu?.currentPageIndex == 0 {
  return (controllerArray[0] as! CustomGestureDelegate).gestureRecognizerShouldRecognizeSimultaneouslyWith(gestureRecognizer, otherGestureRecognizer: otherGestureRecognizer)
  }
  return true
}

您可以为具有表视图的其他子视图控制器添加类似的逻辑。这里我假设只有初始视图控制器有一个表视图。

通过添加以下实现,使您的子视图控制器符合CustomGestureDelegate

class TestTableViewController: UITableViewController, CustomGestureDelegate {
    func gestureRecognizerShouldRecognizeSimultaneouslyWith(_ gestureRecognizer: UIGestureRecognizer, otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    let location = gestureRecognizer.location(in: self.view)
    let path = self.tableView.indexPathForRow(at: location)
    if path != nil {
      gestureRecognizer.isEnabled = false
    }
    gestureRecognizer.isEnabled = true
    return true
  }
}

我希望这对你有所帮助。

更新:

结帐https://github.com/LemonSpike/PageMenu,查看PageMenuDemoStoryboard目标的演示。