2017-10-06 65 views
0

我在一個ViewController和UIView中有兩個UITableView,我試圖實現Peek和Pop,但每次註冊兩個表時,它只能用於第一個表。下面就是我在註冊UITableViews在一個UIViewController中實現偷看和彈出到多個UITableView

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

我想也許嘗試註冊用於預覽只有一次,與源視圖只是被你的觀點:

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

工程就像一個魅力!謝謝! –

相關問題