Reputation: 127
I am trying to pass the name of the organization to a new viewController. The problem is that when I pass the value from the collection view cell to a new label in the other viewController, multiple collectionView cells are displayed that repeat the same information. How would I change this so that once one cell from the first collectionView is selected, only that information will populate the other collectionView cell?
Here is the code for the first viewController.
import UIKit
class FirstViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var secondCollectionView: UICollectionView!
var valueToPass = Int()
var arrayOfOrganizations = ["one", "two", "three", "four"]
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrayOfOrganizations.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let secondCell = secondCollectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
secondCell.nameTextLabel.text = arrayOfOrganizations[indexPath.item]
return secondCell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
valueToPass = indexPath.item
performSegue(withIdentifier: "MoveCollectionViewCell", sender: self)
arrayOfOrganizations.remove(at: indexPath.item)
self.secondCollectionView.deleteItems(at: [indexPath])
secondCollectionView.reloadData()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "MoveCollectionViewCell") {
let destination = segue.destination as! MessagesViewController
destination.valueToPass = arrayOfOrganizations[valueToPass]
}
}
}
class SecondCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var nameTextLabel: UILabel!
@IBOutlet weak var descriptionTextLabel: UILabel!
}
Here is the code for my second viewController.
import UIKit
class MessagesViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var messagesCollectionView: UICollectionView!
var valueToPass = ""
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return valueToPass.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "messagesViewCell", for: indexPath) as! MessagesViewCell
cell.layer.borderWidth = 3
cell.layer.cornerRadius = 12
if (valueToPass == "one") {
cell.textLabel.text = "one"
}
if (valueToPass == "two") {
cell.textLabel.text = "two"
}
if (valueToPass == "three") {
cell.textLabel.text = "three"
}
if (valueToPass == "four") {
cell.textLabel.text = "four"
}
return cell
}
}
class MessagesViewCell: UICollectionViewCell {
@IBOutlet weak var textLabel: UILabel!
}
Upvotes: 0
Views: 367
Reputation: 1
you're numberOfItemsInSection collectionView method returns valueToPass.count changed it to 1.
Upvotes: 0
Reputation: 604
Your numberOfItemsInSection
is returning the size of the string valueToPass (the number of characters), is this what you want? Because If you have only one value the return of that function should be 1.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return valueToPass.count // Number of character in valueToPass
}
Upvotes: 2