gpt4 book ai didi

ios - Peek & Pop 不会仅在最后一个单元格上触发

转载 作者:行者123 更新时间:2023-11-28 05:47:05 25 4
gpt4 key购买 nike

我有一个包含列表的 ProfileVC。我可以单击任何行单元格将显示查看和弹出功能。

ProfileVC.swift

我添加了扩展

extension ProfileViewController : UIViewControllerPreviewingDelegate {

func detailViewController(for indexPath: IndexPath) -> ProfileDetailViewController {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "ProfileDetailViewController") as? ProfileDetailViewController else {
fatalError("Couldn't load detail view controller")
}

let cell = profileTableView.cellForRow(at: indexPath) as! ProfileTableViewCell

// Pass over a reference to the next VC
vc.title = cell.profileName?.text
vc.cpe = loginAccount.cpe
vc.profile = loginAccount.cpeProfiles[indexPath.row - 1]

consoleLog(indexPath.row - 1)

//print("3D Touch Detected !!!",vc)

return vc
}

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
if let indexPath = profileTableView.indexPathForRow(at: location) {

// Enable blurring of other UI elements, and a zoom in animation while peeking.
previewingContext.sourceRect = profileTableView.rectForRow(at: indexPath)

return detailViewController(for: indexPath)
}

return nil
}

//ViewControllerToCommit
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {

// Push the configured view controller onto the navigation stack.
navigationController?.pushViewController(viewControllerToCommit, animated: true)
}

}

然后,在同一个文件 ProfileVC.swift 中的 viewDidLoad() 我注册了它

if (self.traitCollection.forceTouchCapability == .available){
print("-------->", "Force Touch is Available")
registerForPreviewing(with: self, sourceView: view)
}
else{
print("-------->", "Force Touch is NOT Available")
}

结果

我不知道为什么我不能点击 4th 单元格。

该行的最后 单元格不会触发 Peek & Pop。

如何进一步调试它?

最佳答案

您正在将 View Controller 的根 view 注册为 peek 上下文的源 View 。因此,传递给 previewingContext(_ viewControllerForLocation:)` 的 CGPoint 位于该 View 的坐标空间中。

当您尝试从 TableView 中检索相应的行时,该点实际上会根据 TableView 在 Root View 中的相对位置从 TableView 的 frame 中的相应点偏移.

这个偏移量意味着不能为表中的最后一行检索对应的行; indexPathForRow(at:) 返回 nil 并且您的函数返回时不执行任何操作。

您可能还会发现,如果您用力触摸单元格的底部,您实际上会看到下一行。

您可以将 CGPoint 转换为 TableView 的框架,但是在注册预览时将您的 TableView 指定为源 View 更简单:

if (self.traitCollection.forceTouchCapability == .available){
print("-------->", "Force Touch is Available")
registerForPreviewing(with: self, sourceView: self.profileTableView)
}
else{
print("-------->", "Force Touch is NOT Available")
}

关于ios - Peek & Pop 不会仅在最后一个单元格上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54260746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com