如何解决如何在集合视图中更新标签文本?
我正在使用收藏夹视图,一切都很好,我想在收藏夹视图外的文本字段结束编辑所有要更改的标签文本时添加功能。 这是我的代码
我尝试了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,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()
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。