Kre*_*erg 10 objective-c uitableview ios 3dtouch
我的应用程序在Core Data对象中存储不同的属性.它们都显示在UITableView中,如果单击一个单元格,则会显示DetailView.通常的东西,如Master-Detail-XCode-Template.
现在我想用peek和pop实现3D Touch.就像在Mail应用程序中一样,3D触摸一个单元格,进行预览,按下更深处,弹出细节.
到目前为止,我已经完成了这项工作,但我无法弄清楚如何传递相应的数据
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
Run Code Online (Sandbox Code Playgroud)
和
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
Run Code Online (Sandbox Code Playgroud)
到另一个DetailViewController.
如果您只是单击单元格(无3D触摸),我会将数据移交给
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setDetailItem:object];
Run Code Online (Sandbox Code Playgroud)
我正在使用
id detailItem
Run Code Online (Sandbox Code Playgroud)
在每个ViewController中我都设置了相应的对象.
所以我尝试得到类似的东西,所以在我的DetailViewController中,我的"detailItem"需要来自所选(3D触摸)单元格的相应Core Data对象.
感谢帮助 !
如果您使用从单元格到新视图控制器的故事板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
}
}
Run Code Online (Sandbox Code Playgroud)
小智 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
}
Run Code Online (Sandbox Code Playgroud)