简体   繁体   English

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

[英]How to pass a collection view cell's indexPath to another view controller

I'm following a tutorial for making an Instagram-esque app, and the tutorial goes through how to display all data (image, author, likes, etc) all in one collection view. 我正在关注制作类似Instagram的应用程序的教程,该教程介绍了如何在一个集合视图中显示所有数据(图像,作者,喜欢的对象等)。 I'm trying to do it a little bit differently so only the images are displayed in the collection view, then if an image is tapped, the user is taken to a different view controller where the image plus all the other data is displayed. 我试图做一些不同的操作,以便仅在集合视图中显示图像,然后如果轻按图像,则将用户带到另一个视图控制器,在该控制器中将显示图像以及所有其他数据。

So in my view controller with the collection view ( FeedViewController ), I have my array of posts declared outside the class ( Post is the object with all the aforementioned data): 因此,在具有集合视图的视图控制器( FeedViewController )中,我在类外部声明了我的帖子数组( Post是具有所有上述数据的对象):

var posts = [Post]()

Then inside the FeedViewController class, my cellForItemAt indexPath looks like this: 然后在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
}

And then I also obviously have a function to fetch the data which I can post if necessary, but I think the problem is because PhotoDetailController doesn't know the indexPath (although I may be wrong). 然后,我也明显有一个函数来抓取我可以张贴如果有必要的数据,但我认为这个问题是因为PhotoDetailController不知道indexPath(虽然我可能是错的)。

When I run the app and try to view the FeedViewController I get a crash fatal error: unexpectedly found nil while unwrapping an Optional value , highlighting photoDetailController.authorNameLabel line. 当我运行该应用程序并尝试查看FeedViewController出现崩溃fatal error: unexpectedly found nil while unwrapping an Optional value ,突出显示photoDetailController.authorNameLabel行。

Am I correct in assuming the problem is because the indexPath is available only in the collection view data sources within FeedViewController ? 我是否假设问题是正确的,因为indexPath仅在FeedViewController的集合视图数据源中FeedViewController If so, how can I pass that indexPath to PhotoDetailController so my code works correctly? 如果是这样,我怎么能传递到indexPath PhotoDetailController所以我的代码工作正常?

Thanks for any advice! 感谢您的任何建议!

EDIT: Edited my didSelectItem method: 编辑:编辑了我的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)
}

and cellForItemAt : 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
}

And in PhotoDetailController : PhotoDetailController

var selectedPost: Post! inside the class, then: 在课程中,然后:

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

But still getting error use of unresolved identifier "indexPath 但是仍然use of unresolved identifier "indexPath

EDIT 2: Storyboard 编辑2:故事板

在此处输入图片说明

You are getting this nil crash because this photoDetailController is not yet loaded so all its outlet are nil also the way currently you are doing is also wrong. 您收到此nil崩溃,因为这photoDetailController尚未加载,以便其所有的出口都是零也是目前你正在做的方式也是错误的。

You need to add the controller code in didSelectItemAt method and perform the navigation. 您需要在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)
}

Now simply create one instance property of type Post with name selectedPost in PhotoDetailController and set all the detail from this selectedPost object in viewDidLoad of PhotoDetailController . 现在只需创建类型的一个实例属性Post与名selectedPostPhotoDetailController并从该组所有细节selectedPost的对象viewDidLoadPhotoDetailController

Edit: Set your viewDidLoad like this in PhotoDetailController 编辑:设置你viewDidLoad像这样PhotoDetailController

override func viewDidLoad() {
    super.viewDidLoad()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取集合视图单元格的indexPath - How to get the indexPath of a collection view cell Swift 2.1-如何将集合视图单元格的索引行传递给另一个视图控制器 - Swift 2.1 - How to pass index row of collection view cell to another view controller 如何将数据数据从集合视图单元传递到视图控制器 - how to pass data data from collection view cell to view controller 如何在不使用情节提要或IB的情况下从嵌入式集合视图的单元导航到另一个视图控制器? - How to navigate from an embedded collection view’s cell to another view controller without using Storyboard or IB? 按下indexPath单元格时如何导航或显示其他视图控制器 - how to navigate or show other view controller when indexPath cell pressed 通过从Collection View Cell Swift通过segue将Image传递到另一个视图控制器 - Pass Image to another view controller through segue from Collection View Cell Swift 如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器? - How do I pass information from a table view cell in one view controller to another view controller? 如何从表格视图单元格中的图像手势获取 indexPath - How to get indexPath from image gesture in table view's cell 将图像从集合视图控制器快速传递到另一个视图控制器 - swift pass images from collection view controller to another view controller 在swift 3中单击另一个表视图单元格的集合视图数据后,如何在表视图单元格中重新加载集合视图数据? - How to reload the collection view data in table view cell after clicking another table view cell's collection view data in swift 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM