3D Touch和UITableViewCell

6

我在iPhone 6s上的UITableViewController中添加了一些测试3D Touch功能。总体来说,它运行良好,但我发现了两个问题,并不确定发生了什么以及如何以正常的方式进行修复。

1)我的单元格是可选择的,所以用户可以按下它或使用“3D Touch”。问题在于,当我对UITableViewCell使用“Pop”和“Peek”时,它会变成选定状态,并且我无法使用setSelected:setHighlighted:方法以正常的方式取消选择它。我尝试在不同的位置中取消选择甚至在previewingContext:commitViewControllerpresentViewController完成中。它们什么也没做,单元格仍保持选定状态。我通过调用reviewingContext.sourceView 和其他临时代码来检索选定的单元格,但这些方法都没有起作用。

  • 因此问题是:是否有一种正常的方法可以在用户对其应用“Pop”压力时取消选择(或更好地不选择)单元格?

2)我还注意到,有时当我取消“Pop”手势并将单元格恢复到初始状态(当previewingContext:viewControllerForLocation方法甚至未被调用时),我的UI会卡死,并且完全不响应触摸。我需要关闭应用并重新打开才能使其正常工作。这似乎非常奇怪,我检查了这个教程。它运行良好,没有提到的问题,但它们在UITableView上注册委托方法,而不是在单元格上,因此“Pop”手势高亮整个tableView而不是单元格。

  • 有什么想法,这里可能出了什么问题?

这是我如何在我的测试UITableViewController中实现3D Touch,该控制器遵循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)
    }

提前感谢您!

3个回答

4

1

我注意到了同样的问题。 我认为这个问题是由于重复注册引起的。 我添加了标志,然后问题得到了解决。

请尝试这个(我使用Objective-C,所以请用Swift重新编写)。

实现类别

@interface UITableViewCell (FourceTouchRegistered)

@property (nonatomic, assign) BOOL fourceTouchRegistered;

@end

并且

#import "UITableViewCell+FourceTouchRegistered.h"
#import <objc/runtime.h>

@implementation UITableViewCell (FourceTouchRegistered)

- (void)setFourceTouchRegistered:(BOOL)fourceTouchRegistered
{
    objc_setAssociatedObject(self, @"fourceTouchRegistered", @(fourceTouchRegistered), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)fourceTouchRegistered
{
    return [objc_getAssociatedObject(self, @"fourceTouchRegistered") boolValue];
}

@end

那么

    if(cell.fourceTouchRegistered == NO) {
        cell.fourceTouchRegistered = YES;
        if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
            [self registerForPreviewingWithDelegate:self sourceView:cell];
        }
    }

0

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

类似于以下内容:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接