Implement peek and pop to multiple UITableView in one UIViewController

匆匆过客 提交于 2019-12-12 04:43:30

问题


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

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

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


回答1:


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

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

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


来源:https://stackoverflow.com/questions/46601532/implement-peek-and-pop-to-multiple-uitableview-in-one-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!