Segue公司从编程里面的CollectionView
定制的tableView我有两个类:Segue公司从编程里面的CollectionView
class FeedAndGroupsViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {}
class eventsCustomCollectionCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate {}
...其中的CollectionView类创建一个UITableView。
我想在单击tableViewCell时创建一个自定义的segue。我已经为tableView实现了tableView(didSelectRowAt)函数,它工作正常。但是,我无法从此函数调用performSegue。任何人都知道如何解决此问题?
这是因为您的类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
}
}
希望这会有所帮助。
这让我旋转我的*一段时间。在我的情况下,我有一个UITableViewCell和UICollectionView。表格视图单元格包含项目数组。我无法访问ViewController中的指定索引的项目。此外,不使用故事板,所以而不是performSegue(withIdentifier: "Your segue identifier", sender: object)
我用self.navigationController?.pushViewController(detailVC, animated: true)
这是什么结束了为我工作。我不得不在协议功能中传递对象。
protocol NearbyTableViewCellDelegate : class{
func didClick(cell: EatsItemNearbyTableCell, eat: Eats)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let eat = nearByEats[indexPath.row]
delegate?.didClick(cell: self, eat: eat)
}
在视图控制器:
extension EatsItemDetailVC: NearbyTableViewCellDelegate {
func didClick(cell: EatsItemNearbyTableCell, eat: Eats) {
let detailVC = EatsItemDetailVC()
detailVC.eat = eat
detailVC.currentLocation = currentLocation
self.navigationController?.pushViewController(detailVC, animated: true)
}
}
感谢您的帮助!我试过你的解决方案,但似乎我无法在函数cellForItemAt中调用collectionViewCell的委托。 –
您必须调用UICollectionViewCell中UITableViewDelegate的didSelectRowAt中的委托方法。我更新了我的代码。 –
太棒了!非常感谢你 –