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

2024-01-15

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

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

那怎么办呢?


据苹果公司称:

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

然后您可以使用passthroughViews https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPopoverController_class/index.html#//apple_ref/occ/instp/UIPopoverController/passthroughViews通过以下方式:

UICollectionViewController

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

上面的代码是CollectionViewController来处理UICollectionViewController及其所有代表。

集合视图单元格

class CollectionViewCell: UICollectionViewCell {    
    @IBOutlet weak var labelInfo: UILabel!        
}

自定义单元格只有一个UILabel inside.

弹出视图控制器

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

最后是PopoverViewController以以下形式显示.Popover.

我想在回答中指出一些观察结果:

  • 我设置了对该类的引用PopoverViewController使其贯穿整个生命周期,并在其保持打开状态时向其传递数据。

  • 线路var t = self.popoverViewController!.view这是必要的,因为如果不是@IBOutlet在 - 的里面PopoverViewController在它被呈现之前才被初始化,可能还有其他方法可以做到这一点。

  • 我在屏幕中间呈现弹出窗口,以处理多个单元格中的点击,并测试它的滚动,您可以将其显示在您想要的任何位置。

  • 在打开弹出窗口时允许的视图中,我设置了self.view,但通过这种方式,您需要自行关闭它,因为当您在视图中点击时它永远不会被关闭,您可以放置​​任何您想要的视图。

如果您在解决方案中遇到任何问题,我可以在 Github 上分享该项目。

我希望这对你有帮助

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

呈现弹出视图时,如何让用户在父集合视图中选择单元格? 的相关文章

随机推荐