swift - 将 3D Touch 与 Storyboard的 peek 和 pop 结合使用

标签 swift uitableview 3dtouch

我找到了使用 3D Touch 的非常简单的方法——检查 Storyboard中的“Peek & Pop”。但我正在努力解决一个问题。

我有 UITableView,当用户触摸单元格时,我的代码一切正常:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        print(tableView.indexPathForSelectedRow)
    }
}

所以我根据选定的行在我的 Detailed Controller 中填充数据。但是当我用 Peek 或 Pop 方法按下时 tableView.indexPathForSelectedRow 总是返回 nil(嗯..我没有选择行,我只是预览所以 indexPath 是 nil 我猜)。我怎样才能得到那个“偷看”的单元格 indexPath 以将其传递给 segue?

或者 Storyboard 的 Peek & Pop 无法以这种简单的方式工作,我需要在我的代码中完全实现 peek & pop?

最佳答案

可以使用 Storyboard和prepareForSegue 完全实现它。您可以通过使用 sender 对象来做到这一点。

假设您已经直接从 Storyboard中的表格单元格创建了 segue 到下一个 View Controller ,那么发送者对象将是 UITableViewCell 类型。如果您以编程方式触发 segue,那么请记住在方法调用中设置 sender 对象。

您可以使用此单元格从 tableView 中获取 NSIndexPath,类似于以下内容(忽略所有强制展开,这仅用于演示目的):

override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
    let cell = sender as! UITableViewCell
    let indexPath = tableView.indexPath(for: cell)!
    if segue.identifier == "mySegue" {
        let destination = segue.destination as! DetailsViewController
        destination.model = self.model[indexPath.row]
    }
}

关于swift - 将 3D Touch 与 Storyboard的 peek 和 pop 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694178/

相关文章:

swift for windows平台和linux平台开发

ios - 在转换前检查 String 是否有货币符号

ios - 从 uitableview 中删除 + 写入 plist 不起作用

ios - 嵌入式 UITableViewController 相对于 UITableView 的优缺点是什么?

ios - `touchesBegan:withEvent:` 在屏幕左边缘延迟

ios - 3D Touch UICollectionView insideUITableviewCell

ios - 使用字典数据迭代 Dictionary 并将其添加到 swift 数组中

ios - 从标签中获取值并将其添加到另一个标签中

ios - 在 TableView 中删除行后更新标签

ios - 3D Peek and Pop 内存泄漏