kamenr
kamenr

Reputation: 95

UIcollectionview Custom Cell class to didselectitem at

my collectionview contains multiple custom cell class that created programmatically. Fun part is I will have a cell that contains another collectionview inside of it. So how to click into this collectionview to present a new view controller? Below are my sample codes on how to create the cell.

class MainView: UIViewController {
** main view to display UIcollectionview **
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userCellIdent, for: indexPath) as! UserContainerViewCell
            cell.Users = users * to display users array*
            return cell

}


class UserContainerViewCell: UICollectionViewCell{
* the uicollectionview that inside a cell , also contains extension to UIcollectioview datasource and delegate to display the cells within this cell*

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ident, for: indexPath) as! userCell
        let user = featureUsers![indexPath.row]
        cell.config(withUser: user)

        return cell
    }
}



class userCell: UICollectionViewCell {

func config(withUser: UserProfile) { }
** cell class to display the info from the user array **
}

So with this setup, how to click into the cell and present a new controller with the User info in the array?

Upvotes: 0

Views: 753

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You can try

class UserContainerViewCell: UICollectionViewCell {
  var myInstance: MainView!
}

class MainView: UIViewController {
 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userCellIdent, for: indexPath) as! UserContainerViewCell

     cell.myInstance = self

  }

}

then inside didSelectItemAtRow

let vc = myInstance.storyboard?.instantiateViewController(withIdentifier: "infoView") as! InfoController 

vc.providesPresentationContextTransitionStyle = true;

vc.definesPresentationContext = true;

vc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext

myInstance.present(vc, animated: false, completion: nil)

Upvotes: 0

Arnav
Arnav

Reputation: 688

You have to pass a delegate or hit notification to viewController on didSelect of internal collectionViewCell and in that method you can present new view controller .

check out the link

Swift: How to use "didSelectItemAtIndexPath" in UITableViewController(contains UICollectionView)?

Upvotes: 1

Related Questions