[英]Perform a segue selection from a uicollectionview that is embedded in a tableview cell?
Currently we have a uicollectionview that is embedded in a tableview cell. 当前,我们有一个uicollectionview嵌入在一个tableview单元中。 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.
问题是没有选项可以对单元执行segue。 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
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. performSegue
是一个UIViewController方法。 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
从CastCell类中,因为这意味着self.performSegue
,和self
是一个UITableViewCell -它没有performSegue
方法。
The solution, therefore, is to get yourself a reference to the view controller that controls this scene, and call performSegue
on that. 因此,解决方案是让自己获得控制该场景的视图控制器的引用,并在其上调用
performSegue
。
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)
1: A clean method is to create a delegate protocol
inside your UITableViewCell
class and set the UIViewController
as the responder. 1:一种干净的方法是在
UITableViewCell
类内创建delegate protocol
,并将UIViewController
设置为响应者。
2: Once UICollectionViewCell
gets tapped, handle the taps inside the UITableViewCell
and forward the tap to your UIViewController
responder
through delegatation
. 2:一旦
UICollectionViewCell
被挖掘,处理好内部的水龙头UITableViewCell
和水龙头转发给您UIViewController
responder
通过delegatation
。
3: Inside your UIViewController
, you can act on the tap and perform/push/present whatever you want from there. 3:在
UIViewController
,您可以按一下并执行/推动/呈现所需的内容。
You want your UIViewController
to know what is happening, and not call push/presents from "invisible" subclasses that should not handle those methods. 您希望
UIViewController
知道正在发生的事情,而不是从不应处理这些方法的“不可见”子类中调用push / presents。
This way, you can also use the delegate protocol
for future and other methods that you need to forward to your UIViewController
if needed, clean and easy. 这样,您还可以将
delegate protocol
用于将来以及其他需要转移到UIViewController
如果需要),既干净又容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.