将数据从集合视图单元格移动到新的集合视图单元格
Moving data from a collection view cell to a new collection view cell
我正在尝试将组织名称传递给新 viewController。问题是,当我将值从集合视图单元格传递到另一个 viewController 中的新标签时,会显示多个重复相同信息的 collectionView 单元格。我该如何更改它,以便在从第一个 collectionView 中选择一个单元格后,只有该信息将填充另一个 collectionView 单元格?
这是第一个 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!
}
这是我的第二个代码 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!
}
你的numberOfItemsInSection
是return字符串valueToPass的大小(字符数),这是你想要的吗?因为如果您只有一个值,那么该函数的 return 应该是 1.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return valueToPass.count // Number of character in valueToPass
}
你是 numberOfItemsInSection collectionView 方法 returns valueToPass.count 将其更改为 1。
我正在尝试将组织名称传递给新 viewController。问题是,当我将值从集合视图单元格传递到另一个 viewController 中的新标签时,会显示多个重复相同信息的 collectionView 单元格。我该如何更改它,以便在从第一个 collectionView 中选择一个单元格后,只有该信息将填充另一个 collectionView 单元格?
这是第一个 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!
}
这是我的第二个代码 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!
}
你的numberOfItemsInSection
是return字符串valueToPass的大小(字符数),这是你想要的吗?因为如果您只有一个值,那么该函数的 return 应该是 1.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return valueToPass.count // Number of character in valueToPass
}
你是 numberOfItemsInSection collectionView 方法 returns valueToPass.count 将其更改为 1。