简体   繁体   English

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

[英]Peek & pop does not trigger only on the last cell

I have a ProfileVC that contain a list.我有一个包含列表的 ProfileVC。 I can click on any of the row cell will show peek and pop feature.我可以单击任何行单元格将显示查看和弹出功能。

ProfileVC.swift ProfileVC.swift

I added the extention我添加了扩展名

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)
    }
    
}

Then, in the same file ProfileVC.swift in viewDidLoad() I registered it然后,在viewDidLoad()中的同一个文件ProfileVC.swift中我注册了它

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

Result结果

I have no idea why I can not click on the 4th cell.我不知道为什么我不能点击第四个单元格。

The last cell of the row does not trigger Peek & Pop.该行的最后一个单元格不会触发 Peek & Pop。

How would one go about and debug this further?如何进一步调试?

You are registering your view controller's root view as the source view for the peek context.您正在将视图控制器的根view注册为 peek 上下文的源视图。 As a result, the CGPoint that is passed to previewingContext(_ viewControllerForLocation:)` is in the coordinate space of that view.因此,传递给 previewingContext(_ viewControllerForLocation:)` 的CGPoint位于该视图的坐标空间中。

When you try and retrieve the corresponding row from your table view the point will actually be offset from the corresponding point in the table view's frame based on the relative position of the table view in the root view.当您尝试从表格视图中检索相应的行时,该点实际上将根据表格视图在根视图中的相对位置从表格视图frame的相应点偏移。

This offset means that a corresponding row can't be retrieved for the last row in the table;此偏移量意味着无法为表中的最后一行检索相应的行; indexPathForRow(at:) returns nil and your function returns without doing anything. indexPathForRow(at:)返回nil并且您的函数返回而不执行任何操作。

You might also find that if you force touch towards the bottom of a cell you actually get a peek for the next row.您可能还会发现,如果您强制触摸单元格底部,您实际上可以看到下一行。

You could translate the CGPoint into the table view's frame, but it is simpler to just specify your tableview as the source view when you register for previewing:您可以将CGPoint转换为 table view 的框架,但是在注册预览时将 tableview 指定为源视图更简单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM