[英]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.