I added some test 3D Touch
functionality to UITableViewController
on iPhone 6s. In general it works fine, but I was able to observe some 2 problems, and I am not sure what is happening and how to fix that in a normal way.
1) My cell is selectable, so user can press on it or use "3D Touch". The problem is that when I use "Pop" and "Peek" for UITableViewCell
it becomes selected and I can not deselect in a normal way using setSelected:
or setHighlighted:
methods. I tried to deselect it in different places even in previewingContext:commitViewController
in presentViewController
completion. They just do nothing and cells still stay in selected state. I retrieved selected cell by calling reviewingContext.sourceView
and other temp code which gave me selected cell, but these methods didn't work.
2) I also noticed that sometimes when I cancel "Pop" gesture and bring cell to initial state (when previewingContext:viewControllerForLocation
method wasn't even called) my UI just hangs and doesn't respond to touches at all. I need to kill to make it work. It seems very strange, I checked this Tutorial. It works great, without mentioned issues, but they register delegate not on the cell, but on UITableView
so "Pop" gesture highlight the whole tableView, but not the cell.
Here is how I implemented 3D touch in my test UITableViewController
which conforms to 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)
}
Thanks in advance!
I noticed same issue. I think the issue is caused by duplicated registration. I added the flag then the issue is fixed.
Please try this(I'm using Objective-C, so please rewrite in swift).
Implement category that
@interface UITableViewCell (FourceTouchRegistered)
@property (nonatomic, assign) BOOL fourceTouchRegistered;
@end
and
#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
then
if(cell.fourceTouchRegistered == NO) {
cell.fourceTouchRegistered = YES;
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:cell];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With