[英]Swift - Passing data from a CollectionView to a TableViewController
I am learning the new Swift programming language and I would like to know how can I pass data from a CollectionViewController to a TableViewController. 我正在学习新的Swift编程语言,我想知道如何将数据从CollectionViewController传递到TableViewController。
My objective is that everytime someone taps one of the cells in the CollectionViewController, this actions takes them to the TableViewController where a list of images related to the selected cell appears. 我的目标是,每当有人点击CollectionViewController中的一个单元格时,此操作会将它们带到TableViewController,在其中出现与所选单元格相关的图像列表。
CollectionViewController.swift CollectionViewController.swift
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "listSegue") {
if let indexPath = self.menuCollection.indexPathsForSelectedItems() {
let control = (segue.destinationViewController) as TableViewCell
control.cellImage.image = UIImage(named: sectionImages[indexPath])
}
TableViewCell.swift TableViewCell.swift
class TableViewCell: UITableViewCell {
@IBOutlet weak var cellImage: UIImageView!
}
TableViewController.swift TableViewController.swift
Do I have to put some code here?? 我必须在这里放一些代码吗?
Please help me I would really appreciate it :) Thank you :) 请帮助我,我将非常感谢:)谢谢:)
It seems highly unlikely that segue.destinationViewController
is returning a TableViewCell
;-) segue.destinationViewController
返回TableViewCell
;-)的可能性很小
It should be an instance of the UITableViewController
-derived class that should be the segue's destination. 它应该是
UITableViewController
派生类的实例,应该是segue的目的地。
I say " UITableViewController
-derived" because it needs to be a custom class of your design with public properties you can set -- perhaps with values from the selected cell and/or some other state from your CollectionViewController
. 我之所以说“
UITableViewController
派生”,是因为它需要是您的设计的自定义类,并且可以设置公共属性-也许可以使用选定单元格中的值和/或CollectionViewController
其他状态。
cf 比照
Firstly, if your data stays in array you need to know the object that you want pass into prepareForSegue. 首先,如果您的数据保留在数组中,则需要知道要传递给prepareForSegue的对象。 So you may can create a tempVar and assign the value on when it select.
因此,您可以创建一个tempVar并在选择时为其分配值。
private var testObj: TestObj!
func collectionView(collectionView: UICollectionView, didSelectItemAtPath indexPath: NSIndexPath){testObj = self.testObjects[indexPath.item]}
Secondly, you need crate a public API into TableViewCell, the way you can set the value for it. 其次,您需要将公共API创建到TableViewCell中,这是您可以为其设置值的方法。 ie
即
// MARK: Public API
var objTest: ObjTest!
You may need create segue.identifier to check if you have multiple segue. 您可能需要创建segue.identifier来检查是否有多个segue。
if segue.identifier == "Your Segue name" {
let loginSignupVC = segue.destinationViewController as!
YourDestination Controller
//do something here
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
object = self.objects[indexPath.item]
self.performSegueWithIdentifier("Your segue name", sender: self)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.