2016-11-05 76 views
0

定製的tableView我有兩個類:Segue公司從編程裏面的CollectionView

class FeedAndGroupsViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {} 

class eventsCustomCollectionCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate {} 

...其中的CollectionView類創建一個UITableView。

我想在單擊tableViewCell時創建一個自定義的segue。我已經爲tableView實現了tableView(didSelectRowAt)函數,它工作正常。但是,我無法從此函數調用performSegue。任何人都知道如何解決此問題?

回答

0

這是因爲您的類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 
} 
} 

希望這會有所幫助。

+0

感謝您的幫助!我試過你的解決方案,但似乎我無法在函數cellForItemAt中調用collectionViewCell的委託。 –

+0

您必須調用UICollectionViewCell中UITableViewDelegate的didSelectRowAt中的委託方法。我更新了我的代碼。 –

+0

太棒了!非常感謝你 –

0

這讓我旋轉我的輪子一段時間。在我的情況下,我有一個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) 
    } 
}