I have a collection view, and I am trying to get the index of the cell that I am peeking and poping from.
Issue
Currently I am using indexPathForItemAtPoint:
however this always returns 0
no mater where I tap on the screen.
Code
collection view controller:
- (void)viewDidLoad {
[super viewDidLoad];
[self registerForPreviewingWithDelegate:self sourceView:self.collectionView];
}
- (UIViewController *) previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
CellEditViewController *CEVC = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"]; //The view I want peek/pop to
NSLog(@"Location: %f,%f", location.x, location.y);
NSLog(@"Index of: %lu", [[self.collectionView indexPathForItemAtPoint:location] row]);
[CEVC setPreferredContentSize:CGSizeMake(0.0, 320.0)];
return CEVC;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
[self showViewController:viewControllerToCommit sender:self];
}
What I have tried
- Creating a new location to identify the index cell.
- Moving
registerForPreviewingWithDelegate:sourceView:
to where I create each cell. - Moving
previewingContext:viewControllerForLocation:
andpreviewingContext:commitViewController:
to the cell view method, this did not work for other reasons.
I do not think this is an issue with previewing, because when I implemented the same thing with a UITapGestureRecognizer
, I got a similar output:
Tap recognizer:
- (void) processDoubleTap:(UITapGestureRecognizer *)sender {
NSLog(@"Got tapped twice");
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint point = [sender locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
NSLog(@"Index was: %lu", [indexPath row]);
if (indexPath) {
NSLog(@"Index was double tapped");
}
}
}
Output:
2017-12-25 10:48:13.990523-0800 quickthings[3052:356150] Got tapped twice
2017-12-25 10:48:13.990843-0800 quickthings[3052:356150] Index was: 0
Source
Screenshot
Here is what does happen, this is exactly what I want. The only other thing I would like to do is when the cell is tapped also be able to get the index of the tapped cell.
Collection View In Story Board
The (blue) UIView
is "linked" to the Collection View Controller (the top view controller in the second screenshot below).