3D Touch Peek and Pop from UITableViewCell how to hand over data to other UIViewController?

前端 未结 3 504
天命终不由人
天命终不由人 2020-12-14 11:59

my app stores different attributes in Core Data objects. They are all shown in a UITableView, if you click a cell, a DetailView is shown. the usual stuff like in the Master-

3条回答
  •  轮回少年
    2020-12-14 12:30

    Base on Dheeraj's reply, adjust some code to rectify the wrong touch location of tableview cell. Assume we need pass the value named type to destination controller.

    - (nullable UIViewController *)previewingContext:(id )previewingContext viewControllerForLocation:(CGPoint)location{
    // check if we're not already displaying a preview controller
    if ([self.presentedViewController isKindOfClass:[DetailViewController class]]) {
        return nil;
    }
    
    CGPoint cellPostion = [self.tableview convertPoint:location fromView:self.view];
    NSIndexPath *path = [self.tableview indexPathForRowAtPoint:cellPostion];
    
    if (path) {
        UITableViewCell *cell = [self.tableview cellForRowAtIndexPath:path];
        type = [[self.data objectAtIndex:path.row] integerValue];
        // shallow press: return the preview controller here (peek)
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        DetailViewController *previewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
        previewController.type = type;
        previewingContext.sourceRect = [self.view convertRect:cell.frame fromView:self.tableview];
        return previewController;
    }
    return nil;
    

    }

提交回复
热议问题