[英]How to pass a collection view cell's indexPath to another view controller
我正在关注制作类似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的集合视图数据源中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:故事板
您收到此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
在PhotoDetailController
并从该组所有细节selectedPost
的对象viewDidLoad
的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.