2014-11-05 68 views
1

我正在学习新的Swift编程语言,我想知道如何将数据从CollectionViewController传递到TableViewController。Swift - 将数据从CollectionView传递到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

我一定要在这里把一些代码?

请帮助我,我真的很感激它:)谢谢:)

回答

0

似乎极不可能的,segue.destinationViewController返回一个TableViewCell ;-)

应该UITableViewController的一个实例 - 派生类应该是segue的目的地。

我说“UITableViewController-derived”,因为它需要是您的设计的自定义类,您可以设置公有属性 - 可能来自所选单元格的值和/或来自您的CollectionViewController的某个其他状态。

CF

+0

哦,我明白了:)你知道我怎么能从UITableViewCell访问我的cellImage变量?我想在我的CollectionViewController中访问这个变量 – ketokun 2014-11-05 23:18:30

+0

你不能 - 它看起来像你的'TableViewCall'类,因此这就是你必须放在你的'UICollectionView'中 - 这是一个教程(尽管在Objective C):http://skeuo.com/uicollectionview-custom-layout-tutorial – 2014-11-05 23:26:56

0

首先,如果阵列中的数据保持,你需要知道你要传递到prepareForSegue的对象。所以你可以创建一个tempVar并在选择时赋值。其次,您需要创建一个公共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) 
} 
相关问题