我有一个关于UICollectionView的基本问题,并将数据传递给多个视图控制器,具体取决于按下的单元格。
在ViewController.swift中 - 我拥有在控制器视图中显示单元格的所有代码。如你所见,我有一个小的打印线测试,告诉我哪个单元格(布局,最终)被轻敲。然后将我转到另一个由uniqueViewController.swift管理的视图控制器。
这个视图控制器(uniqueViewController.swift)我有一个标签(uniqueLabel),我想根据从ViewController点击的布局来改变文本。这是我难倒的数据部分的全部传递。任何代码片段和指导将不胜感激!感谢。
ViewController.swift
import UIKit
class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var data = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
data = ["Layout one", "Layout two", "Layout three", "Layout four"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("my cell", forIndexPath: indexPath) as! UICollectionViewCell
var btnLabel = cell.viewWithTag(1) as! UILabel
btnLabel.text = data[indexPath.row]
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//println("Cell \(indexPath.row) tapped")
performSegueWithIdentifier("testSegue", sender: self)
if((indexPath.row) == 0) {
println("Layout 1 clicked")
}
else if((indexPath.row) == 1) {
println("Layout 2 clicked")
}
else if((indexPath.row) == 2) {
println("Layout 3 clicked")
}
else if((indexPath.row) == 3) {
println("Layout 4 clicked")
}
}
}
uniqueViewController.swift
import UIKit
class uniqueViewController: UIViewController {
@IBOutlet weak var uniqueLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
uniqueLabel.text = "I want this labels text unique depending on which cell was tapped from ViewController.swift"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
**更新**
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//println("Cell \(indexPath.row) tapped")
performSegueWithIdentifier("testSegue", sender: indexPath.row)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let vc = segue.destinationViewController as? uniqueViewController {
switch(sender as! Int) {
case 0:
vc.uniqueLabel.text = "First"
break
case 1:
vc.uniqueLabel.text = "Second"
break
case 2:
vc.uniqueLabel.text = "Third"
break
case 3:
vc.uniqueLabel.text = "Fourth"
break
default:
println("Index out of range.")
}
}
}
答案 0 :(得分:6)
您应该在prepareForSegue
CollectionViewController
实施例:
您必须在uniqueViewController
中创建一个变量,以保存从prepareForSegue
分配的缓存字符串值
class uniqueViewController : UIViewController {
var clickedCellValue: String!
override func viewDidLoad(){
super.viewDidLoad()
uniqueLabel.text = clickedCellValue
}
}
在CollectionViewController中,实现以下内容:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let vc = segue.destinationViewController as? uniqueViewController {
switch(sender as! Int) {
case 0:
vc.clickedCellValue = "First"
break
case 1:
vc.clickedCellValue = "Second"
break
case 2:
vc.clickedCellValue = "Third"
break
case 3:
vc.clickedCellValue = "Fourth"
break
default:
println("Index out of range.")
}
}
}
为了能够检索点击的索引,请将indexPath.row
作为发件人传递:
performSegue("testSegue", sender: indexPath.row)
注意: 我不是说你应该这样做,但这应该让你知道如何实现自己的解决方案。您可以将发送方传递给uniqueViewController并让控制器将索引转换为字符串,这取决于您构建数据源的方式。
希望这会有所帮助:)