是否可以从单元格获取索引路径项?

问题描述 投票:0回答:1

所以我有一个具有节标题的集合视图。在该标题中,我还有另一个水平滚动的集合视图。我想在此集合视图中获取用户,并能够将视图控制器推送到所选用户的个人资料页面。

我创建了一个委托,因为我无法从可重用的视图中推送视图控制器。这是那个代表。

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)

       }

这是上面代码中的问题。通常,我只是抓住名为peopleToFollow的配置文件的索引路径项,然后将用户推送到正确的配置文件,例如:

        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

但是我无法在集合视图的didSelect中编写该代码,因为集合视图位于集合视图(可重复使用的视图)的节标题中

如何获得所选单元格的indexpath并将该代码放入委托中?

ios swift uicollectionview pushviewcontroller
1个回答
0
投票

尝试tableView.indexPath(for: someCell)

© www.soinside.com 2019 - 2024. All rights reserved.