Monday, 30 September 2019

How to grab indexPath item from a collection view cell for a delegate?

So I have a collection view that has a section header. In that header I have another collection view that scrolls horizontally. I want to fetch a fixed amount of users in this collection view and be able to push the view controller to the profile page of the selected user.

I have created a delegate since I cannot push view controllers from a reusable view. Here is that delegate.

protocol PeopleToFollowDelegate {
    func handleProfileTapped(for cell: FeedReusableView)
}
  // Create function to push users to the correct profile when selected
    func handleProfileTapped(for header: FeedReusableView) {

        print("profile tapped, push user to the correct profile page")
       let header = FeedReusableView()

        //try to grab the indexpath item so the corrext data is pushed
        let user = header.peopleToFollow // ??? What goes here? I cannot grab index path

            let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

           let profileViewController = storyBoard.instantiateViewController(withIdentifier: "profileViewController") as? ProfileViewController
            profileViewController?.user? = user


            self.navigationController?.pushViewController(profileViewController!, animated: true)

       }

This is where I have issues in the code above. Normally I would just grab the index path item of the profiles called peopleToFollow and push the user to the correct profile like this

        var peopleToFollow = [User]()

        let user = peopleToFollow[indexPath.item]
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let profileViewController = storyBoard.instantiateViewController(withIdentifier: "profileViewController") as? ProfileViewController


        profileViewController?.user = user

        // Then push to next controller

However I cannot write that code in didSelect of the collection view because the collection view is in a section header of collection view ( Reusable view)

How can I get the indexpath for the selected cell and put that code in the delegate?



from How to grab indexPath item from a collection view cell for a delegate?

No comments:

Post a Comment