在呈现弹出窗口视图时,如何让用户在父集合视图中选择单元格?

web*_*ets 8 ios uipopover uicollectionview swift

我有一个集合视图,当选择一个单元格时,它会显示一个弹出视图,显示有关该单元格的更多信息.

我想允许用户单击另一个单元格,然后将弹出窗口视图更改为显示该单元格的信息,而无需关闭弹出窗口.如果用户要单击父视图上不是单元格的某个位置,则弹出窗口应该关闭.但是,我希望用户仍能够在不关闭弹出框的情况下滚动集合视图.

怎么办?

Vic*_*ler 11

根据Apple的说法:

当弹出窗口处于活动状态时,通常会禁用与其他视图的交互,直到弹出窗口被取消.为此属性分配视图数组允许弹出框外部的点击由相应的视图处理.

然后您可以使用passthroughViews以下方式:

CollectionViewController

import UIKit

let reuseIdentifier = "Cell"

class CollectionViewController: UICollectionViewController {

   var popoverViewController : PopoverViewController?

   override func viewDidLoad() {
       super.viewDidLoad()         

   }

   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
   }   

   override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
      //#warning Incomplete method implementation -- Return the number of sections
      return 1
   }    

   override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      //#warning Incomplete method implementation -- Return the number of items in the section
      return 15
   }

   override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
      cell.labelInfo.text = "Cell \(indexPath.row)"        
      return cell
   }

   override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

      println("tapped")

      if let popover = self.popoverViewController {

          var cell = self.collectionView!.cellForItemAtIndexPath(indexPath) as! CollectionViewCell
          popover.labelPop.text = cell.labelInfo.text

      }
      else {

          self.popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PopoverViewController") as? PopoverViewController

          var cell = self.collectionView!.cellForItemAtIndexPath(indexPath) as! CollectionViewCell

          var t = self.popoverViewController!.view
          self.popoverViewController!.labelPop.text = cell.labelInfo.text

          self.popoverViewController!.modalPresentationStyle = .Popover
          var popover = self.popoverViewController!.popoverPresentationController


          popover?.passthroughViews = [self.view]
          popover?.sourceRect = CGRect(x: 250, y: 500, width: 0, height: 0)
          self.popoverViewController!.preferredContentSize = CGSizeMake(250, 419)

          popover!.sourceView = self.view

          self.presentViewController(self.popoverViewController!, animated: true, completion: nil)
      }
   }    
}
Run Code Online (Sandbox Code Playgroud)

上面的代码是CollectionViewController处理它UICollectionViewController及其所有代理.

CollectionViewCell

class CollectionViewCell: UICollectionViewCell {    
    @IBOutlet weak var labelInfo: UILabel!        
}
Run Code Online (Sandbox Code Playgroud)

只有一个UILabel内部的自定义单元格.

PopoverViewController

class PopoverViewController: UIViewController {

   @IBOutlet var labelPop: UILabel!

   override func viewDidLoad() {
      super.viewDidLoad()

      // Do any additional setup after loading the view.
   }

   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
   }   
}
Run Code Online (Sandbox Code Playgroud)

最后PopoverViewController以形式展示.Popover.

我想指出一些观察结果:

  • 我设置了对类的引用以PopoverViewController使其保持整个生命周期并在数据保持打开时传递数据.

  • 这条线var t = self.popoverViewController!.view是必要的,因为如果不是@IBOutlet内部,PopoverViewController它不是init,直到它呈现,可能有其他方法来做到这一点.

  • 我在屏幕中间显示弹出框以处理几个单元格中的点击并测试它的滚动,您可以在任何您想要的位置显示它.

  • 在打开popover时允许的视图中,我设置了self.view,但是这样你需要为你自己解雇它,因为当你在视图中点击它时它永远不会被解雇,你可以放置你想要的任何视图.

您对解决方案有任何麻烦我可以在Github上分享它的项目.

我希望这对你有帮助


pte*_*fil 5

你要找的是popover 的passthroughViews属性.

但是,如果您通过点击单元格打开弹出窗口,我不会看到如何滚动collectionView有意义.你不打开指向你的细胞的箭头弹出窗口?滚动视图将使呈现单元移开...