ios - Peek&Pop for UITableViewCell in a master-detail app

标签 ios swift uikit

我有一个来自 RayWenderlich 的示例应用程序:UISearchController Tutorial: Getting Started - 请获取 finished version

在这个项目中,我们在 TableView 中有一个糖果列表,如果我们点击一​​行,我们将获得该糖果的详细信息 View 。为了使详细信息 View 正常工作,我们使用选定的糖果设置 DetailViewControllerdetailCandy 属性。

我想添加 peek 和 pop 功能只是为了好玩。我尝试了简单的方法:在 Storyboard中启用Preview & Commit Segues 并等待它开箱即用。它确实用适当的 segue 调用了 prepare(for:sender:) 方法,但不幸的是我似乎无法确定 tableView 中的 selectedCandy > 本身。

  1. tableViewindexPathForSelectedRownil
  2. 可以通过获取此属性使用私有(private) API(快速修复)解决此问题:tableView.value(forKey: "pendingSelectionIndexPath") as!索引路径
  3. 我知道我可以在 UIViewControllerPreviewingDelegate 的帮助下从代码手动实现 peek&pop,我的问题就消失了。

有人知道如何轻松优雅地解决这个问题吗?

最佳答案

prepare(for:sender:) 中使用 tableView.indexPath(for: sender as! UITableViewCell) 完成这项工作。

这是我的 prepare(for:sender:) 的样子:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {

    var indexPath = tableView.indexPathForSelectedRow
    if indexPath == nil {
        indexPath = tableView.indexPath(for: sender as! UITableViewCell)
    }

  if let indexPath = indexPath {
    let candy: Candy
    if isFiltering() {
      candy = filteredCandies[indexPath.row]
    } else {
      candy = candies[indexPath.row]
    }
    let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
    controller.detailCandy = candy
    controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
    controller.navigationItem.leftItemsSupplementBackButton = true
  }
 }
}

它检查单元格是否被选中或用户是否正在查看并相应地进行调整。

关于ios - Peek&Pop for UITableViewCell in a master-detail app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52727176/

相关文章:

ios - 收到错误 : FT_Open_Face failed: error 2

ios - swift:计算字典中的重复值

swift - 多个纹理之间的淡入淡出,SpriteKit

ios - 在 TableView 之间切换

javascript - 尝试打开未安装的 native 应用程序时如何防止 iOS Safari 警报?

ios - 如何从常规 UIFont 创建粗体 UIFont?

swift - RawOptionsSetType 在 Xcode6 的 Swift 中引发错误

swift - UISearchResultsUpdating 不根据 searchBar 文本过滤 collectionView

ios - 如何在场景中添加按钮和标签

iphone - keyboardWillShowNotification 边缘案例