2015-09-24 57 views
4

我试图在我的应用程序中实现窥视和流行功能。但由于我还无法测试它,我想知道如果我正确地做了这件事,我觉得有什么不对吗?3D触摸偷看和流行麻烦

- (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; 
} 

回答

6

我目前正在实施这个在我们的应用程序,看起来大多是正确的。我遇到的一个问题是,在位置给你的坐标是UIViewController的视图,而不是UITableView。根据您的设置,它们可能是相同的,但在我的情况下,我需要将位置转换为UITableView的坐标。

CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView]; 

当然,您的里程可能会有所不同。

+0

即时通讯与您自己一样,坐标不正确,因为它没有考虑状态栏的存在。 – DevC

1

您的代码的唯一问题是获取转换位置的方式不正确。

即使在滚动表格视图后,下面的代码也会得到正确的位置。

CGPoint convertedLocation = [self.tableView convertPoint:location fromView:self.view]; 

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation]; 
1

如果您使用正确的sourceView注册了previewDelegate,则不需要更正位置。因此,而不是

[self registerForPreviewingWithDelegate:self sourceView:self.view]; 

要注册这样说:

[self registerForPreviewingWithDelegate:self sourceView:self.tableView]; 

然后位置,你从委托调用获得需要滚动/ contentOffset考虑。

+0

不好意思,这似乎不适用于tableView,但是适用于collectionView。 – FrankWest

+0

这不是tableView的答案,但是阅读这个问题确切地解决了我在使用UICollectionView(和Xamarin.iOS)时遇到的问题。非常酷的答案导致为别人解决另一个问题。 – Tahari