您好,我正在使用 swift 并且我正在尝试在选择集合视图单元格时更改集合视图单元格的标签
Hello, I am using swift and I am trying to change a label of a collection view cell when the a collection view cell is selected
我这里有我的集合视图控制器,当我单击集合视图单元格时,它在 true 和 false 之间更新,但我的标签没有从添加更新为未添加。任何帮助将不胜感激我已经在这里工作了 4 天。我已经尝试阅读苹果文档,但它没有给出有关如何使用布尔值完成此操作的示例。我想要发生的是当按下集合视图单元格时,我的集合视图单元格标签顶部的标签从添加更改为不添加。我知道它是有效的,因为当我按下集合视图单元格时用打印语句测试它时,它从真到假来回变化。我只是在将标签从添加更改为不添加时遇到问题。
import UIKit
class ShoppingListCollectionViewController: UICollectionViewController {
var shoppingItemController = ShoppingItemController()
var shoppingItemCollectionViewCell = ShoppingItemCollectionViewCell()
override func viewDidLoad() {
super.viewDidLoad()
shoppingItemCollectionViewCell.updateViews()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShoppingListDetailSegue" {
guard let shoppingListDetailVC = segue.destination as? ShoppingListDetailViewController else {
return
}
}
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
// MARK: UICollectionViewDataSource
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return shoppingItemController.shoppingItems.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ShoppingItemCell", for: indexPath) as? ShoppingItemCollectionViewCell else {
fatalError("Collection view cell identifier is wrong or the cell is not a ShoppingItemCollectionViewCell")
}
// Configure the cell
let shoppingListItem = shoppingItemController.shoppingItems[indexPath.item]
cell.imageView.image = shoppingListItem.image
cell.shoppingItemLabel.text = shoppingListItem.imageName
if shoppingListItem.added == true {
cell.hasBeenAddedLabel.text = "Added"
} else {
cell.hasBeenAddedLabel.text = "Not Added"
}
return cell
}
// MARK: UICollectionViewDelegate
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
var chosenItem = shoppingItemController.shoppingItems[indexPath.item]
chosenItem.added = !chosenItem.added
shoppingItemController.shoppingItems[indexPath.item] = chosenItem
if chosenItem.added == true {
chosenItem.updateViews()
}
print(chosenItem.added)
}
您只需要从 didSelectItemAt
方法中的 cellForItem(at:)
方法获取单元格,并将标签文本更新为您想要的字符串:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! ShoppingItemCollectionViewCell
var chosenItem = shoppingItemController.shoppingItems[indexPath.item]
chosenItem.added.toggle()
cell.label.text = chosenItem.added ? "Added" : "Not Added"
}
我这里有我的集合视图控制器,当我单击集合视图单元格时,它在 true 和 false 之间更新,但我的标签没有从添加更新为未添加。任何帮助将不胜感激我已经在这里工作了 4 天。我已经尝试阅读苹果文档,但它没有给出有关如何使用布尔值完成此操作的示例。我想要发生的是当按下集合视图单元格时,我的集合视图单元格标签顶部的标签从添加更改为不添加。我知道它是有效的,因为当我按下集合视图单元格时用打印语句测试它时,它从真到假来回变化。我只是在将标签从添加更改为不添加时遇到问题。
import UIKit
class ShoppingListCollectionViewController: UICollectionViewController {
var shoppingItemController = ShoppingItemController()
var shoppingItemCollectionViewCell = ShoppingItemCollectionViewCell()
override func viewDidLoad() {
super.viewDidLoad()
shoppingItemCollectionViewCell.updateViews()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShoppingListDetailSegue" {
guard let shoppingListDetailVC = segue.destination as? ShoppingListDetailViewController else {
return
}
}
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
// MARK: UICollectionViewDataSource
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return shoppingItemController.shoppingItems.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ShoppingItemCell", for: indexPath) as? ShoppingItemCollectionViewCell else {
fatalError("Collection view cell identifier is wrong or the cell is not a ShoppingItemCollectionViewCell")
}
// Configure the cell
let shoppingListItem = shoppingItemController.shoppingItems[indexPath.item]
cell.imageView.image = shoppingListItem.image
cell.shoppingItemLabel.text = shoppingListItem.imageName
if shoppingListItem.added == true {
cell.hasBeenAddedLabel.text = "Added"
} else {
cell.hasBeenAddedLabel.text = "Not Added"
}
return cell
}
// MARK: UICollectionViewDelegate
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
var chosenItem = shoppingItemController.shoppingItems[indexPath.item]
chosenItem.added = !chosenItem.added
shoppingItemController.shoppingItems[indexPath.item] = chosenItem
if chosenItem.added == true {
chosenItem.updateViews()
}
print(chosenItem.added)
}
您只需要从 didSelectItemAt
方法中的 cellForItem(at:)
方法获取单元格,并将标签文本更新为您想要的字符串:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! ShoppingItemCollectionViewCell
var chosenItem = shoppingItemController.shoppingItems[indexPath.item]
chosenItem.added.toggle()
cell.label.text = chosenItem.added ? "Added" : "Not Added"
}