5

我正在尝试在我的应用程序中实现 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;
}
4

3 回答 3

6

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

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

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

于 2015-09-30T15:38:20.513 回答
3

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

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

你应该这样注册:

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

然后,您从委托调用中获得的位置将滚动/ contentOffset 考虑在内。

于 2016-02-09T03:13:51.803 回答
1

您的代码的唯一问题是获得 convertLocation 的方式不正确。

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

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

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
于 2015-11-19T05:45:20.610 回答