简体   繁体   English

偷看未在UITableViewCell中被调用

[英]Peek not being called in UITableViewCell

I'm currently implement Peek and Pop in my app. 我目前正在我的应用程序中实现Peek and Pop。 I have a UITableView containing some data. 我有一个UITableView包含一些数据。 I wrote this function to handle a Peek: 我编写了此函数来处理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;
}

The UITableView is constructed like this: 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];
}

However, right now, it's not even logging Test to the console. 但是,现在甚至还没有将Test记录到控制台。 What am I doing wrong? 我究竟做错了什么?

Did you add this delegate to your ViewController 您是否将此委托添加到您的ViewController

UIViewControllerPreviewingDelegate UIViewControllerPreviewingDelegate

Also you need to register for Force Touch 您还需要注册Force Touch

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM