从嵌入在 tableview 单元格中的 uicollectionview 执行 segue 选择? [英] Perform a segue selection from a uicollectionview that is embedded in a tableview cell?

查看:20
本文介绍了从嵌入在 tableview 单元格中的 uicollectionview 执行 segue 选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我们有一个嵌入在 tableview 单元格中的 uicollectionview.选择Collection View单元格时,它假设将推扣为另一个视图控制器启动.问题是没有选项可以在单元格上执行 segue.有办法解决吗?这是单元格:

Currently we have a uicollectionview that is embedded in a tableview cell. When the collection view cell is selected it's suppose to initiate a push segue to another view controller. The problem is there is no option to perform the segue on the cell. Is there a way around it? Here is the cell:

class CastCell : UITableViewCell {

  var castPhotosArray: [CastData] = []

  let extraImageReuseIdentifier = "castCollectCell"
  let detailToPeopleSegueIdentifier = "detailToPeopleSegue"

  var castID: NSNumber?

  @IBOutlet weak var castCollectiontView: UICollectionView!

  override func awakeFromNib() {
    castCollectiontView.delegate = self
    castCollectiontView.dataSource = self
  }
}


extension CastCell: UICollectionViewDataSource {

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return castPhotosArray.count
  }

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = castCollectiontView.dequeueReusableCell(withReuseIdentifier: extraImageReuseIdentifier, for: indexPath) as! CastCollectionViewCell

    cell.actorName.text = castPhotosArray[indexPath.row].name     
    return cell
  }
}

extension CastCell: UICollectionViewDelegate {

     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      self.castID = castPhotosArray[indexPath.row].id

     performSegue(withIdentifier: detailToPeopleSegueIdentifier, sender: self) //Use of unresolved identifier 'performSegue' error

    }
}


extension CastCell {
   func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let peopleVC = segue.destination as! PeopleDetailViewController

    peopleVC.id = self.castID
  }
}

推荐答案

问题是没有选项可以在单元格上执行转场

The problem is there is no option to perform the segue on the cell

没有单元格上的转场"这样的东西.segue 是从一个视图控制器到另一个视图控制器.performSegue 是一个 UIViewController 方法.所以你不能在你的 CastCell 类中说 performSegue,因为这意味着 self.performSegue,而 self 是一个 UITableViewCell——它没有 performSegue 方法.

There is no such thing as a "segue on a cell". A segue is from one view controller to another. performSegue is a UIViewController method. So you cannot say performSegue from within your CastCell class, because that means self.performSegue, and self is a UITableViewCell — which has no performSegue method.

因此,解决方案是获得对控制此场景的视图控制器的引用,并在其上调用 performSegue.

The solution, therefore, is to get yourself a reference to the view controller that controls this scene, and call performSegue on that.

在像您这样的情况下,我喜欢获取此参考的方式是沿着响应者链向上走.因此:

In a situation like yours, the way I like to get this reference is by walking up the responder chain. Thus:

var r : UIResponder! = self
repeat { r = r.next } while !(r is UIViewController)
(r as! UIViewController).performSegue(
    withIdentifier: detailToPeopleSegueIdentifier, sender: self)

这篇关于从嵌入在 tableview 单元格中的 uicollectionview 执行 segue 选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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