I'm trying to implement peek and pop feature in my application.but since I can't test it yet, I wanted to know if I was doing it correctly, I feel something is wrong?
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
NSLog(@"TEST");
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:location];
if (indexPath) {
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
NSDictionary *dict = [self.array objectAtIndex:indexPath.row];
cell.textLabel.text = [dict objectForKey:@"name"];
DetailViewController *previewController = [[DetailViewController alloc] init];
// previewController.content = content;
previewingContext.sourceRect = cell.frame;
return previewController;
}
return nil;
}
I'm currently implementing this in our app and that looks mostly right. A problem I encountered is that the coordinates given to you in location are for the UIViewController's view, not the UITableView. Depending on your setup, they may be the same, but in my case, I needed to convert the location to the UITableView's coordinates.
CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
Of course, your mileage may vary.
You should not need to correct the location if you registered the previewDelegate with the correct sourceView. So instead of
[self registerForPreviewingWithDelegate:self sourceView:self.view];
you should register it this way:
[self registerForPreviewingWithDelegate:self sourceView:self.tableView];
Then location you get from the delegate call takes scrolling / contentOffset into account.
The only problem with your code is the way to get convertedLocation is not right.
Below code will get correct location even after you scroll the table view.
CGPoint convertedLocation = [self.tableView convertPoint:location fromView:self.view];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
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