简体   繁体   English

如何制作一个tableview的多个实例?

[英]How to make multiple instances of a tableview?

I have a collectionView of images so that when an image is clicked a tableview is presented. 我有一个图像的collectionView,以便单击图像时显示一个tableview。 My tableview displays posts that are queried from my parse server, but when I go back to my collection view and click on a different image, I am taken to a view controller displaying the same data. 我的表视图显示了从解析服务器查询的帖子,但是当我返回到集合视图并单击其他图像时,将被带到显示相同数据的视图控制器。

I am trying to get each collectionview image's table view to query its own unique set of posts. 我试图获取每个collectionview图像的表视图,以查询其自己独特的帖子集。

How do I create a separate instance of tableview for each collection view image? 如何为每个集合视图图像创建一个单独的tableview实例?

The code I have in my collection view's didSelectItemAtIndexPath: 我在集合视图中的didSelectItemAtIndexPath代码:

    override func collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath) {

    collectionView.allowsMultipleSelection = false

     // navigate to preference view
    let post = self.storyboard?.instantiateViewControllerWithIdentifier("myTableView") as! myTableView
    self.navigationController?.pushViewController(post, animated: true)

In your implementation of didSelectItemAtIndexPath , you never use the indexPath parameter. didSelectItemAtIndexPath的实现中,从不使用indexPath参数。 It simply means that your code doesn't care about which cell was selected and you will always have the same view controller with the same data presented. 这仅表示您的代码不关心选择哪个单元格,并且您将始终具有相同的视图控制器,并提供相同的数据。

In order to have it to work, in the didSelectItemAtIndexPath method, keep a reference to the indexPath selected, or even in the data corresponding To this indexpath (the data you actually use in cellForItemAtIndexPath ). 为了使其工作,请在didSelectItemAtIndexPath方法中保留对所选indexPath的引用,或者甚至保留与此索引路径对应的数据(您在cellForItemAtIndexPath实际使用的数据)的cellForItemAtIndexPath

Then, in your view controller containing the collectionView, override prepareForSegue and use it to pass the data to the ViewController with the tableview before it is pushed. 然后,在包含collectionView的视图控制器中,重写prepareForSegue并使用它在推送数据之前将其与tableview一起传递给ViewController。 More info from UIViewController Class Reference . 更多信息,请参见UIViewController类参考

You should have a variable in your 'myTableView' and give it a value before 'pushViewController'. 您应该在“ myTableView”中有一个变量,并在“ pushViewController”之前给它一个值。 Something like that: 像这样的东西:

override func collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath) {

collectionView.allowsMultipleSelection = false
//GET CLICKED
var YOUROBJECT = YOURARRAYOFIMAGES.objectAtIndex(indexPath.row)
 // navigate to preference view
let postView = self.storyboard?.instantiateViewControllerWithIdentifier("myTableView") as! myTableView
postView.post = YOUROBJECT.post
self.navigationController?.pushViewController(postView, animated: true)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM