[英]Swift - Passing data from a CollectionView to a TableViewController
我正在学习新的Swift编程语言,我想知道如何将数据从CollectionViewController传递到TableViewController。
我的目标是,每当有人点击CollectionViewController中的一个单元格时,此操作会将它们带到TableViewController,在其中出现与所选单元格相关的图像列表。
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
class TableViewCell: UITableViewCell {
@IBOutlet weak var cellImage: UIImageView!
}
TableViewController.swift
我必须在这里放一些代码吗?
请帮助我,我将非常感谢:)谢谢:)
segue.destinationViewController
返回TableViewCell
;-)的可能性很小
它应该是UITableViewController
派生类的实例,应该是segue的目的地。
我之所以说“ UITableViewController
派生”,是因为它需要是您的设计的自定义类,并且可以设置公共属性-也许可以使用选定单元格中的值和/或CollectionViewController
其他状态。
比照
首先,如果您的数据保留在数组中,则需要知道要传递给prepareForSegue的对象。 因此,您可以创建一个tempVar并在选择时为其分配值。
private var testObj: TestObj!
func collectionView(collectionView: UICollectionView, didSelectItemAtPath indexPath: NSIndexPath){testObj = self.testObjects[indexPath.item]}
其次,您需要将公共API创建到TableViewCell中,这是您可以为其设置值的方法。 即
// MARK: Public API
var objTest: ObjTest!
您可能需要创建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.