【问题标题】:How to pass a collection view cell's indexPath to another view controller如何将集合视图单元格的 indexPath 传递给另一个视图控制器
【发布时间】:2017-03-09 00:50:15
【问题描述】:

我正在学习制作 Instagram 风格应用程序的教程,该教程介绍了如何在一个集合视图中显示所有数据(图像、作者、喜欢等)。我试图做一些不同的事情,所以只有图像显示在集合视图中,然后如果点击图像,用户将被带到另一个视图控制器,其中显示图像和所有其他数据。

所以在我的带有集合视图 (FeedViewController) 的视图控制器中,我在类外部声明了我的帖子数组(Post 是包含所有上述数据的对象):

var posts = [Post]()

然后在FeedViewController 类中,我的cellForItemAt indexPath 看起来像这样:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell

    // creating the cell
    cell.postImage.downloadImage(from: posts[indexPath.row].pathToImage)

    photoDetailController.authorNameLabel.text = posts[indexPath.row].author
    photoDetailController.likeLabel.text = "\(posts[indexPath.row].likes!) Likes"
    photoDetailController.photoDetailImageView.downloadImage(from: posts[indexPath.row].pathToImage)

    return cell
}

然后我显然也有一个函数来获取我可以在必要时发布的数据,但我认为问题是因为PhotoDetailController 不知道 indexPath(尽管我可能错了)。

当我运行应用程序并尝试查看FeedViewController 时,我遇到了崩溃fatal error: unexpectedly found nil while unwrapping an Optional value,突出显示photoDetailController.authorNameLabel 行。

我是否正确假设问题是因为 indexPath 仅在 FeedViewController 内的集合视图数据源中可用?如果是这样,我如何将该 indexPath 传递给 PhotoDetailController 以便我的代码正常工作?

感谢您的建议!

编辑:编辑了我的didSelectItem 方法:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let photoDetailController = self.storyboard?.instantiateViewController(withIdentifier: "photoDetail") as! PhotoDetailController

    photoDetailController.selectedPost = posts[indexPath.row]

    self.navigationController?.pushViewController(photoDetailController, animated: true)
}

cellForItemAt:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell


    cell.postImage.downloadImage(from: posts[indexPath.row].pathToImage)


    return cell
}

PhotoDetailController:

var selectedPost: Post! 在类里面,那么:

override func viewDidLoad() {
    super.viewDidLoad()

    self.authorNameLabel.text = selectedPost[indexPath.row].author
    self.likeLabel.text = "\(selectedPost[indexPath.row].likes!) Likes"
    self.photoDetailImageView.downloadImage(from: selectedPost[indexPath.row].pathToImage)
}

但仍然出现错误use of unresolved identifier "indexPath

编辑 2:故事板

【问题讨论】:

  • 使用集合视图的 didSelectItemAtIndexPath 并像我们对表格视图一样执行 segue。

标签: ios swift uicollectionview


【解决方案1】:

你得到这个nil 崩溃是因为这个photoDetailController 还没有加载所以它的所有出口都是零而且你现在做的方式也是错误的。

您需要在didSelectItemAt方法中添加控制器代码并进行导航。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let photoDetailController = self.storyboard?.instantiateViewController(withIdentifier: "YourIdentifier") as! PhotoDetailController  
    photoDetailController.selectedPost = posts[indexPath.row]
    self.navigationController?.pushViewController(photoDetailController, animated: true)
}

现在只需在PhotoDetailController 中创建一个名为selectedPostPost 类型的实例属性,并在viewDidLoadPhotoDetailController 中设置此selectedPost 对象的所有细节。

编辑:像这样在PhotoDetailController中设置viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    self.authorNameLabel.text = selectedPost.author
    self.likeLabel.text = "\(selectedPost.likes!) Likes"
    self.photoDetailImageView.downloadImage(from: selectedPost.pathToImage)
}

【讨论】:

  • 您的意思是在您的代码中包含author 吗?我不确定那条线在做什么。但无论如何,我应该将var post: Post! 放在PhotoDetailController 类中?
  • @KingTim 这里的作者是什么,你有没有从cellForRowAt 方法中删除代码?
  • 是的,我从cellForItemAt 方法中删除了代码。作者是发布图像的用户。我说的是你的回答中的photoDetailController.selectedPost = posts[indexPath.row].author 行——作者应该在那里吗?
  • @KingTim 这是我的拼写错误,应该简单地photoDetailController.selectedPost = posts[indexPath.row] 检查编辑后的答案。
  • 很酷,这就是我的想法 - 我编辑了我的问题以显示我现在在 FeedViewController 中拥有的内容。到目前为止是正确的吗?现在在PhotoDetailController,所以我理解正确,我将var selectedPost: Post! 放入类中,然后将self.authorNameLabel.text = selectedPost[indexPath.row].author 等放入viewDidLoad?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
相关资源
最近更新 更多