在一个UIViewController

时间:2017-10-06 08:29:29

标签: ios swift uitableview swift3 3dtouch

我在一个ViewController中有两个UITableView和一个UIView,我正在尝试实现Peek和Pop,但每次我注册这两个表时,它只适用于第一个表。以下是我正在注册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
        }

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

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

感谢任何帮助。过去4小时我一直在低头。

1 个答案:

答案 0 :(得分:1)

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

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

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

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