Swift 通过集合视图单元格在ViewController之间传递数据
我有一个关于UICollectionView和将数据传递给多个视图控制器的相当基本的问题,这取决于按下哪个单元格 在ViewController.swift中-我拥有在控制器视图中显示单元格的所有代码。正如你们所看到的,我有一个小的打印行测试,它告诉我哪个单元格(布局,最终)被点击。然后将我转到另一个视图控制器,该视图控制器由uniqueViewController.swift管理 正是这个视图控制器(uniqueViewController.swift)有一个标签(uniqueLabel),我希望文本根据从ViewController单击的布局进行更改。这是让我感到困惑的整个数据传递部分。任何代码片段和指导将不胜感激!谢谢 ViewController.swiftSwift 通过集合视图单元格在ViewController之间传递数据,swift,uicollectionview,cell,Swift,Uicollectionview,Cell,我有一个关于UICollectionView和将数据传递给多个视图控制器的相当基本的问题,这取决于按下哪个单元格 在ViewController.swift中-我拥有在控制器视图中显示单元格的所有代码。正如你们所看到的,我有一个小的打印行测试,它告诉我哪个单元格(布局,最终)被点击。然后将我转到另一个视图控制器,该视图控制器由uniqueViewController.swift管理 正是这个视图控制器(uniqueViewController.swift)有一个标签(uniqueLabel),我
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")
}
}
}
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.
}
}
uniqueViewController.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")
}
}
}
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.")
}
}
}
您应该将
prepareforsgue
实现到您的CollectionViewController
例如:
您必须在uniqueViewController
中创建一个变量,以保存从prepareforsgue
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,并让控制器将索引转换为字符串,这取决于数据源的结构
希望这有帮助:)您应该将
prepareforsgue
实现到您的CollectionViewController
例如:
您必须在uniqueViewController
中创建一个变量,以保存从prepareforsgue
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,并让控制器将索引转换为字符串,这取决于数据源的结构
希望这有帮助:)谢谢你的回复。我已经用你的代码更新了我原来的帖子。vc.uniqueLabel.text=“First”上仍有错误-致命错误:在展开可选值(lldb)时意外发现零,感谢您的回复。我已经用你的代码更新了我原来的帖子。vc.uniqueLabel.text=“First”上仍有错误-致命错误:在展开可选值(lldb)时意外发现为零