本文介绍了在一个 UIViewController 中实现 peek 和 pop 到多个 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 ViewController 和一个 UIView 中有两个 UITableView,我正在尝试实现 Peek 和 Pop,但是每次我注册这两个表时,它都只能工作对于第一张桌子.以下是我注册 UITableViews 的地方:

I have two UITableView in one ViewController and a UIView, and I'm trying to implement Peek and Pop but every time I register both the tables, it only works for the first table. Following is where I'm registering the UITableViews:

if traitCollection.forceTouchCapability == UIForceTouchCapability.available {
    registerForPreviewing(with: self, sourceView: self.tableView2)
    registerForPreviewing(with: self, sourceView: self.tableView)
}

我的viewControllerForLocation:

        if  tableView.point(inside: tableViewPoint, with: nil) {
            guard let indexPath = self.tableView.indexPathForRow(at: location) else { return nil }
            guard let cell = self.tableView.cellForRow(at: indexPath) else { return nil }
            let storyBoard = UIStoryboard(name: "Home", bundle: nil)
            guard let previewViewController = storyBoard.instantiateViewController(withIdentifier: "article_page") as? ArticleViewController else { return nil }
            previewViewController.preferredContentSize = CGSize(width: 0.0, height: 550)

            previewViewController.passedValue = article_ids[(indexPath as NSIndexPath).row]

            previewingContext.sourceRect = cell.frame
            return previewViewController
        } else {
            guard let indexPath = self.tableView2.indexPathForRow(at: location) else { return nil }
            guard let cell = self.tableView2.cellForRow(at: indexPath) else { return nil }
            let storyBoard = UIStoryboard(name: "Home", bundle: nil)
            guard let previewViewController = storyBoard.instantiateViewController(withIdentifier: "article_page") as? ArticleViewController else { return nil }
            previewViewController.preferredContentSize = CGSize(width: 0.0, height: 550)
            if indexPath.row == 0 {
                previewViewController.passedValue = 1001
            } else {
                previewViewController.passedValue = weekly_article_id_[(indexPath as NSIndexPath).row - 1]
            }
            previewingContext.sourceRect = cell.frame
            return previewViewController
        }

以上方法无效.(它可能是错误的实现导致它对我没有任何意义).我也尝试了这个解决方案,但没有按预期工作.我正在寻找的是这样的:

The above doesn't work. (Its probably the wrong implementation cause its not making any sense to me). I tried this solution as well but its not working as expected. What I'm looking for is something like this:

if sourceView == tableView {
//first tableView implementation
} else {
//second tableView implementation
}

感谢任何帮助.在过去的 4 小时里,我一直在头疼.

Any help is appreciated. I have been cracking my head for the last 4hrs.

推荐答案

我可能会尝试只注册一次预览,源视图只是您的视图:

I'd perhaps try registering for previewing only once, with the source view just being your view:

registerForPreviewing(with: self, sourceView: self.view)

然后在您的 previewingContext 方法中将此点转换为每个 tableView 以确定它是哪个:

Then in you previewingContext method convert this point to each tableView to determine which one it is:

let tableViewPoint = self.view.convert(location, to: self.tableView)
if let indexPath1 = self.tableView.indexPathForRow(at: tableViewPoint) {
    // Point is within first table - get the cell at this indexPath and handle
}
else {
    let tableViewPoint2 = self.view.convert(location, to: self.tableView2)
    if let indexPath2 = self.tableView2.indexPathForRow(at: tableViewPoint2) {
        // Point is within second table - get the cell at this indexPath and handle
    }
}

这篇关于在一个 UIViewController 中实现 peek 和 pop 到多个 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:16
查看更多