Peek没有在UITableViewCell中调用

时间:2015-10-17 12:37:41

标签: ios objective-c uitableview 3dtouch

我目前正在我的应用中实现Peek和Pop。我有UITableView包含一些数据。我写了这个函数来处理Peek:

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    // check if we're not already displaying a preview controller
    if ([self.presentedViewController isKindOfClass:[DetailViewController class]]) {
        return nil;
    }

    NSLog(@"Test");

    CGPoint cellPostion = [self.tableView convertPoint:location fromView:self.view];
    NSIndexPath *path = [self.tableView indexPathForRowAtPoint:cellPostion];

    if (path) {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
        NSDictionary *data = [self.restaurants objectAtIndex:path.row];
        // shallow press: return the preview controller here (peek)
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        DetailViewController *previewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
        previewController.restaurant = data;
        [previewingContext setSourceRect:cell.frame];
        return previewController;
    }
    return nil;
}

UITableView的构造如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.title = @"Overview";

    self.restaurants = [sRestaurants fetchAll];
    NSLog(@"%@", self.subscriptions);

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

但是,现在,它甚至没有将Test记录到控制台。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您是否已将此委托添加到ViewController

UIViewControllerPreviewingDelegate

您还需要注册Force Touch

if (IS_OS_9_OR_LATER) {
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }
}