[英]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
与名selectedPost
在PhotoDetailController
并从该组所有细节selectedPost
的对象viewDidLoad
的PhotoDetailController
。
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.