从UITableViewCell 3D Touch Peek和Pop如何将数据移交给其他UIViewController?

时间:2015-10-07 21:22:20

标签: ios objective-c uitableview 3dtouch

我的应用程序在Core Data对象中存储不同的属性。它们都显示在UITableView中,如果单击一个单元格,则会显示DetailView。通常的东西,如Master-Detail-XCode-Template。

现在我想用偷看和弹出来实现3D Touch。就像在Mail应用程序中一样,3D触摸一个单元格,进行预览,按下更深处,弹出细节。

到目前为止,我已经完成了这项工作,但我无法弄清楚如何在

中传递相应的数据
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit

到另一个DetailViewController。

如果你只是点击单元格(没有3D触摸),我将移交

中的数据
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    [[segue destinationViewController] setDetailItem:object];

我正在使用

id detailItem

在每个ViewController中,我将相应的对象设置为它。

所以我尝试得到类似的东西,所以在我的DetailViewController中,我的“detailItem”需要来自所选(3D触摸)单元格的相应Core Data对象。

感谢您的帮助!

3 个答案:

答案 0 :(得分:8)

如果您使用从单元格到新视图控制器的故事板segue,则发件人是表格视图单元格。

所以你可以这样实现prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([sender isKindOfClass:[UITableViewCell class]]) {

        UITableViewCell *cell = sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

        id viewController = segue.destinationViewController;

        // Get your data object
        // Pass it to the new view controller
    }
}

答案 1 :(得分:6)

使用故事板ID从故事板中获取目标View控制器,并在prepareForSegue方法中传递您正在执行的对象。

下面是我用来传递数据的代码。它在Swift中,它应该在目标C中类似。如果你想要Objective C版本,请告诉我。

func previewingContext(previewingContext: UIViewControllerPreviewing,viewControllerForLocation location: CGPoint) -> UIViewController? method:

    // Obtain the index path and the cell that was pressed.
    guard let indexPath = tableView.indexPathForRowAtPoint(location),
              cell = tableView.cellForRowAtIndexPath(indexPath) else { return nil }

    // Create a destination view controller and set its properties.
    guard let destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("DestinationViewController") as? DestinationViewController else { return nil }
    let object = fetchedResultController.objectAtIndexPath(indexPath)
    destinationViewController.detailItem = object

    /*
        Set the height of the preview by setting the preferred content size of the destination view controller. Height: 0.0 to get default height
    */
    destinationViewController.preferredContentSize = CGSize(width: 0.0, height: 0.0)

    previewingContext.sourceRect = cell.frame

    return destinationViewController
}

答案 2 :(得分:2)

根据Dheeraj的回复,调整一些代码以纠正tableview单元的错误触摸位置。假设我们需要将名为type的值传递给目标控制器。

- (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;
}

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;

}