2015-10-17 44 views
1

我目前在我的應用中實現Peek和Pop。我有一個UITableView包含一些數據。我寫了這個函數來處理偷看:在UITableViewCell中沒有調用窺視

- (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到控制檯。我究竟做錯了什麼?

回答

0

你今天委託添加到您的ViewController

UIViewControllerPreviewingDelegate

還需要爲部隊觸摸

if (IS_OS_9_OR_LATER) { 
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { 
     [self registerForPreviewingWithDelegate:self sourceView:self.view]; 
    } 
} 
註冊
相關問題