这是因为您的类eventsCustomCollectionCell是UICollectionViewCell子类而不是UIViewController子类。
func performSegue(withIdentifier identifier: String, sender: Any?)
是在UIViewController中可用的方法。因此,对于解决方案,您可以创建一个eventsCustomCollectionCell协议,可以有一个方法
func cellClicked(cell: eventsCustomCollectionCell)
和你FeedAndGroupsViewController可以实现此协议,并可以调用performSegue。
我已经为您的用例编写了框架代码,您可以参考此代码并开始使用。
class EventsCustomCollectionCell: UICollectionViewCell,UITableViewDelegate{
weak var delegate : EventsCustomCollectionCellDelegate?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.didClick(cell: self)
}
}
protocol EventsCustomCollectionCellDelegate : class{
func didClick(cell:EventsCustomCollectionCell)
}
class FeedAndGroupsViewController: UIViewController,EventsCustomCollectionCellDelegate,UICollectionViewDataSource{
var collectionView : UICollectionView!
var yourArrayForCollectionView = [String]()
func didClick(cell:EventsCustomCollectionCell){
if let index = collectionView.indexPath(for:cell){
let object = yourArrayForCollectionView[index.row]
performSegue(withIdentifier: "Your segue identifier", sender: object)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Your cell reuse id", for: indexPath) as! EventsCustomCollectionCell
cell.delegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return yourArrayForCollectionView.count
}
}
希望这会有所帮助。
感谢您的帮助!我试过你的解决方案,但似乎我无法在函数cellForItemAt中调用collectionViewCell的委托。 –
您必须调用UICollectionViewCell中UITableViewDelegate的didSelectRowAt中的委托方法。我更新了我的代码。 –
太棒了!非常感谢你 –