我正在尝试在我的应用程序中实现peek和pop功能。但是因为我还不能测试它,我想知道我是否正确地做了,我觉得有些不对劲?
- (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;
}
答案 0 :(得分:6)
我目前正在我们的应用中实现这一点,看起来大部分是正确的。我遇到的一个问题是在位置给你的坐标是UIViewController的视图,而不是UITableView。根据您的设置,它们可能是相同的,但在我的情况下,我需要将位置转换为UITableView的坐标。
CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
当然,您的里程可能会有所不同。
答案 1 :(得分:2)
如果您使用正确的sourceView注册了previewDelegate,则无需更正位置。而不是
[self registerForPreviewingWithDelegate:self sourceView:self.view];
你应该这样注册:
[self registerForPreviewingWithDelegate:self sourceView:self.tableView];
然后,您从委托调用获得的位置需要考虑scrolling / contentOffset。
答案 2 :(得分:1)
您的代码唯一的问题是获取convertLocation的方法不对。
即使滚动表格视图,下面的代码也会获得正确的位置。
CGPoint convertedLocation = [self.tableView convertPoint:location fromView:self.view];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];