Nitesh Nitesh - 2 months ago 9
iOS Question

PerfomSegue's in UITableViewCell Class

I have

TableView
and inside
TableViewCell
I have added
UICollectionView
. Now when user taps on any
UICollectionView
, I want to pass the data to next
viewController
but unfortunately I cannot use perform segue inside UITableViewCell class where I have didselect item. Is there any way to perform segue over here ?

Whole Code

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)
}

}

Answer

Create one protocol, after that create its instance inside TableViewCell and implement the protocol in the UserLibraryViewController, after that in the didSelectItemAtIndexPath of CollectionView use that delegate instance to call the method like this.

protocol CustomCollectionDelegate {
    func selectedCollectionCell(indexPath: NSIndexPath)
}

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

Now call this method inside your didSelectItemAtIndexPath of CollectionView.

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

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

Now implement this protocol in UserLibraryViewController like this and set delegate in the 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)
    }
}

Note: I have added protocol method with NSIndexPath as parameter you can change it with your custom object or with Dictionary/Array.

Comments