3D Touch和UITableViewCell

B.S*_*.S. 6 swift ios9 3dtouch

我在iPhone 6s上添加了一些测试3D Touch功能UITableViewController.一般来说它工作正常,但我能够观察到一些问题,我不知道发生了什么以及如何以正常方式解决这个问题.

1)我的单元格是可选的,因此用户可以按下它或使用"3D Touch".问题是,当我使用"Pop"和"Peek"时,UITableViewCell它会被选中,我无法使用setSelected:setHighlighted:方法以正常方式取消选择.我尝试在不同的地方取消选择,即使previewingContext:commitViewControllerpresentViewController完成时也是如此.他们什么都不做,细胞仍处于选定状态.我通过调用reviewingContext.sourceView和其他临时代码检索了选定的单元格,这些代码给了我选定的单元格,但这些方法不起作用.

  • 所以问题是当用户在其上应用"Pop"时,有一种正常的方法来取消选择(或更好地选择)单元格吗?

2)我还注意到,有时当我取消"Pop"手势并将单元格置于初始状态时(当previewingContext:viewControllerForLocation方法甚至没有被调用时),我的UI就会挂起并且根本不响应触摸.我需要杀死才能使它发挥作用.看起来很奇怪,我查了一下这个教程.它工作得很好,没有提到问题,但是它们不在单元格上注册委托,但是UITableView这样"Pop"手势突出显示整个tableView,而不是单元格.

  • 有什么想法,这里有什么不对吗?

以下是我在测试中实现3D触摸的UITableViewController方法UIViewControllerPreviewingDelegate

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("SomeDemoID",
        forIndexPath: indexPath) as! UITableViewCell

    // Some cell configuration

    if #available(iOS 9.0, *) {
        if traitCollection.forceTouchCapability == .Available {
            self.registerForPreviewingWithDelegate(self, sourceView: cell)
        }
    } else { // Fallback on earlier versions}

    return cell;
}


// MARK: - UIViewControllerPreviewingDelegate

    func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?
    {
        // Just Test View Controller
        return UIViewController(nibName: nil, bundle: nil)
    }

    func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController)
    {
        self.presentViewController(viewControllerToCommit, animated: true, completion: nil)
    }
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Yog*_*ana 5

  • 在tableview单元格中添加3D Touch的简便方法.没有一行代码

故事板图像


小智 0

尝试检查您的预览 UIViewController 是否已在您的previewingContext(_:viewControllerForLocation:)方法中呈现。

就像是

func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?
{
    if self.navigationController.presentedViewController.isKindOfClass(PreviewViewController) {
        return nil
    }
    return PreviewViewController()
}
Run Code Online (Sandbox Code Playgroud)