swift - 如何更新集合视图中的标签文本?
问题描述
我正在使用集合视图,一切顺利,我想在集合视图之外的文本字段结束编辑所有要更改的标签文本时添加功能。这是我的代码
我尝试了 newValue() 函数的变体,但没有运气。
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate, UITextFieldDelegate {
var lastSelectedIndexPath:IndexPath?
@IBOutlet weak var txtx: UITextField!
var text2 = ["new text", "text2", "text3"]
override func viewDidLoad() {
let toolBar = UIToolbar()
toolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneButtonClicked))
toolBar.setItems([flexibleSpace,doneButton], animated: false)
doneButton.style = .done
doneButton.tintColor = UIColor.white // should be white
super.viewDidLoad()
ItemCollection.delegate = self
ItemCollection.dataSource = self
txtx.delegate = self
txtx.inputAccessoryView = toolBar
}
@objc func doneButtonClicked() {
view.endEditing(true)
}
// Store the clicked item location
private var section: Int = 0
private var item0 = 0
func newValue() {
//new func here
}
@IBAction func save() {
}
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var ItemCollection: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return text2.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = ItemCollection.dequeueReusableCell(withReuseIdentifier: "check", for: indexPath) as? checkCollectionViewCell
cell?.isSelected = (lastSelectedIndexPath == indexPath)
cell?.layer.backgroundColor = colorLiteral(red: 0.1068892553, green: 0.119746305, blue: 0.1270511448, alpha: 1)
cell?.layer.cornerRadius = 10
cell?.backgroundColor = UIColor(red: 0/256, green: 128/256, blue: 255/256, alpha: 0.66)
cell?.az.text = text2[indexPath.row]
cell?.chechButton.backgroundColor = .clear
return cell!
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// text2[indexPath.row] = "Selected"
// self.ItemCollection.reloadItems(at: [indexPath])
guard lastSelectedIndexPath != indexPath else{
return
}
if lastSelectedIndexPath != nil {
ItemCollection.deselectItem(at: lastSelectedIndexPath!, animated: false)
}
let selectedCell = collectionView.cellForItem(at: indexPath) as! checkCollectionViewCell
selectedCell.isSelected = true
lastSelectedIndexPath = indexPath
}
}
我应该添加什么功能来通过保存按钮更新所有文本。?谢谢)!
解决方案
- 在数组中添加所有更改的值(代码中的 text2)
- 一旦你更新值调用这个方法
self.collectionview.reloadData()
[编辑]
func reload(){
self.text2 = ["new data1", "newd ata2" , "new data3"]
self.collectionView.reloadData()
}
推荐阅读
- .htaccess - 如何从 php codeigniter 中的 URL 隐藏 ID 和控制器?
- python - 从python Beautifulsoup中的span标签获取内容
- string - 带有十六进制代码的字符串与 Charset TComPort 的问题
- html - 在闪亮仪表板的动态框标题中转义 HTML
- python - 使用 np.where 和 np.select 的条件语句
- docker - Docker kill 命令不起作用:无法杀死容器
- amazon-web-services - 在 AWS 上重用 VPC 的最佳实践是什么?
- git - 从索引和目录中删除文件后,git log 中的文件会发生什么
- pytest - Pytest 使用带有夹具的参数化
- javascript - Total 在购物车中显示 NaN