Angga
Angga

Reputation: 39

change view controller from didSelectItemAt custom CollectionViewCell inside a custom TableViewCell

I have a collection view inside tableview cell, so I want to select the collection view cell and it goes to another view controller. So how do i do that?

I have tried myself, it's either does nothing, "Application tried to present modally an active controller", or "Attempt to present .. on .. whose view is not in the window hierarchy".

Upvotes: 0

Views: 137

Answers (1)

Sharad Chauhan
Sharad Chauhan

Reputation: 4891

Add a delegate in cell class (Outside of cell class) and declare variable inside the class :

protocol CellSelectedDelegate { //Name them as you want
    func cellSelected()
}
class TableCell: UITableViewCell {
    var delegate: CellSelectedDelegate?
}

Then in cell's didSelectItem :

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
   delegate?.cellSelected()
}

Now go to controller class where you have tableView datasource and delegate methods (assuming they are in controller class and not in another view) and add this in cellForItem method :

cell.delegate = self

And last part, implement custom delegate method in controller class :

extension YourController: CellSelectedDelegate {
    func cellSelected() {
       //Present next controller here
    }
}

Upvotes: 2

Related Questions