【发布时间】:2020-01-26 06:06:43
【问题描述】:
我有一个包含部分标题的 collectionView。在该部分的标题中,我添加了另一个 collectionView,它显示推荐的配置文件供用户关注,并且它会水平滚动。
我无法从 ReuseableView(包含第二个集合视图)推送视图控制器,因此我创建了一个协议,该协议将包含一个可以为我推送视图控制器的函数。
当我点击个人资料(单元格)时,它会将我推送到我自己的个人资料。因为我无法获取另一个collectionView 中的indexPath.item。
协议
- 这里我创建了保存函数
HandleProfileTapped的协议,名为FeedReuseableView的单元格是保存第二个集合视图的sectionHeader 的名称。
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 = [User]()是用户将在部分标题中看到的推荐配置文件。 如果集合视图不在节标题中,那么我会像这样轻松获取 indexPath
// When profile is tapped, push user to userProfile
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
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 ( profile)
}
总而言之,我只需要一种方法来获取 indexPath.item 以实现该功能。
编辑
这也是我在 FeedReuseableView 中使用的功能
var delegate: PeopleToFollowDelegate?
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
// Return the number of profiles
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return peopleToFollow.count
}
//Display the recommended profiles
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "profilesCell", for: indexPath) as! PeopleToFollowCollectionCell
cell.user = peopleToFollow[indexPath.item]
return cell
}
// When profile is tapped, push user to userProfile
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
profilesTapped()
}
@objc func profilesTapped() {
delegate?.handleProfileTapped(for: self)
}
【问题讨论】:
-
你为什么不把它传进去 protocol PeopleToFollowDelegate { func handleProfileTapped(for cell: FeedReusableView, indexPath:IndexPath) }
-
我不知道如何传递索引路径
-
@AbuUlHassan 或者我真的应该说,我什至不知道在 indexPath 代码中放什么。
-
分享你使用协议函数的方式和位置
handleProfileTapped(: FeedReusableView) -
@Andrew21111 完成先生
标签: ios swift uicollectionview pushviewcontroller