呈现弹出窗口视图时,如何让用户在父集合视图中选择单元格? [英] When presenting a popover view, how can I let the user select cell in parent collection view?

查看:97
本文介绍了呈现弹出窗口视图时,如何让用户在父集合视图中选择单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合视图以及当选择的小区它是显示关于该小区的更多信息一酥料饼的图.

I have a collection view and when a cell is selected it presents a popover view showing more information about that cell.

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

I would like to allow the user to click another cell and then have the popover view change to showing that cell's information without having to close the popover. If the user were to click somewhere on the parent view that isn't a cell then the popover should close. But, I would like the user to still be able to scroll the collection view without closing the popover.

那怎么办?

推荐答案

Apple认为:

当弹出框处于活动状态时,通常会禁用与其他视图的交互,直到关闭该弹出框为止.为该属性分配视图数组,将允许弹出窗口外部的轻拍由相应的视图处理.

When a popover is active, interactions with other views are normally disabled until the popover is dismissed. Assigning an array of views to this property allows taps outside of the popover to be handled by the corresponding views.

然后,您可以使用

Then you can use the passthroughViews in the following way :

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

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

The above code is the CollectionViewController to handle the UICollectionViewController and all its delegates.

CollectionViewCell

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

内部仅包含UILabel的自定义单元格.

The custom cell with just a UILabel inside.

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

最后是PopoverViewController.Popover的形式显示.

我想指出一些答案:

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

  • I set a reference to the class PopoverViewController to keep it through the life cycle and pass it data when it remains open yet.

var t = self.popoverViewController!.view行是必要的,因为如果PopoverViewController内的@IBOutlet直到出现时才被初始化,否则还有其他方法可以实现.

The line var t = self.popoverViewController!.view it's necessary because if not the @IBOutlet inside the PopoverViewController was not init until it's presented, there could be other ways to do it.

我在屏幕中间显示弹出窗口,以处理多个单元格中的点击,并对其滚动进行测试,您可以在任意位置显示它.

I present the popover in the middle of the screen to handle the tap in several cell and test it the scroll too, you can display it in any position you want.

在允许打开弹出窗口的视图中,我设置了self.view,但是您需要自己关闭它,因为在视图中轻按时它不会被关闭,您可以放任何您想要的视图.

In the views to allow when the popover is opened , I set the self.view, but in this way you need to dismiss it for you own, because it never is dismissed when you make taps in the view, you can put any view you want instead.

解决方案有任何麻烦,我可以在Github上共享该项目.

Any trouble you have with the solution I can share it the project on Github.

希望对您有帮助

这篇关于呈现弹出窗口视图时,如何让用户在父集合视图中选择单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆