如何将集合视图单元的indexPath传递给另一个视图控制器

时间:2017-03-08 16:13:01

标签: ios swift uicollectionview

我正在学习制作Instagram式应用程序的教程,本教程将介绍如何在一个集合视图中显示所有数据(图像,作者,喜欢等)。我尝试稍微不同,所以只有图像显示在集合视图中,然后如果点击图像,用户将被带到另一个视图控制器,在那里显示图像和所有其他数据。 / p>

所以在我的视图控制器中使用集合视图(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:故事板

enter image description here

1 个答案:

答案 0 :(得分:3)

你的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)
}

现在只需在Post中创建一个名为selectedPost的{​​{1}}类型的实例属性,并在PhotoDetailController selectedPost中设置此viewDidLoad对象的所有详细信息{1}}。

修改:在PhotoDetailController

中设置viewDidLoad这样的内容
PhotoDetailController
相关问题