2016-09-07 50 views
1

我有TableViewTableViewCell我添加了UICollectionView。現在當用戶點擊任何UICollectionView時,我想將數據傳遞給下一個viewController,但不幸的是,我不能在UITableViewCell類中使用執行segue,而我已經選擇了item。有沒有什麼辦法可以在這裏執行segue?PerfomSegue在UITableViewCell類

整個代碼

class UserLibraryViewController: UIViewController { 

extension UserLibraryViewController : UITableViewDelegate { } 

extension UserLibraryViewController : UITableViewDataSource { 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return myBooksGenre[section] 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return myBooksGenre.count 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell 
    cell.tableIndexPath = indexPath 

    return cell 
} 
} 

TableViewCell

class UserLibraryTableViewCell: UITableViewCell { 
@IBOutlet weak var collectionView: UICollectionView! 
var tableIndexPath: NSIndexPath? 

} 

extension UserLibraryTableViewCell : UICollectionViewDataSource { 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return 12 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("userBooksCollectionViewCell", forIndexPath: indexPath) as! UserLibraryCollectionViewCell 



    return cell 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

    print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ") 


} 
} 

extension UserLibraryTableViewCell : UICollectionViewDelegateFlowLayout { 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    let itemsPerRow:CGFloat = 4 
    let hardCodedPadding:CGFloat = 5 
    let itemWidth = (collectionView.bounds.width/itemsPerRow) - hardCodedPadding 
    let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding) 
    return CGSize(width: itemWidth, height: itemHeight) 
} 

} 

回答

2

創建一個協議,之後裏面TableViewCell創建它的實例和實施在UserLibraryViewController協議,之後在的didSelectItemAtIndexPathCollectionView使用該委託實例來調用像這樣的方法。

protocol CustomCollectionDelegate { 
    func selectedCollectionCell(indexPath: NSIndexPath) 
} 

class UserLibraryTableViewCell: UITableViewCell { 
    @IBOutlet weak var collectionView: UICollectionView! 
    var tableIndexPath: NSIndexPath? 
    var delegate: CustomCollectionDelegate? 
} 

現在調用這個方法您的CollectionViewdidSelectItemAtIndexPath內。

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

    print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ") 
    self.delegate?.selectedCollectionCell(indexPath) 
} 

現在實施UserLibraryViewController這樣該協議並設置委託在cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell 
    cell.tableIndexPath = indexPath 
    cell.delegate = self 
    return cell 
} 

extension UserLibraryViewController : CustomCollectionDelegate { 
    func selectedCollectionCell(indexPath: NSIndexPath) { 
     self.performSegueWithIdentifier("Identifier", sender: indexPath) 
    } 
} 

注:我已經加入協議的方法與NSIndexPath作爲參數,你可以用你的自定義對象或Dictionary/Array改變它。

+0

不是。擴展沒有被調用。試圖放置一個斷點。 – Nitesh

+0

對不起,我忘記了用self來設置委託,查看'cellForRowAtIndexPath'的編輯答案。 –

+0

非常感謝。現在按照我的要求工作。 :) – Nitesh