Swift 如何添加Func以更新集合视图单元格的所有标签文本?
我正在使用集合视图,一切进展顺利,我想在集合视图之外的文本字段结束编辑所有要更改的标签文本时添加功能。Swift 如何添加Func以更新集合视图单元格的所有标签文本?,swift,uicollectionview,Swift,Uicollectionview,我正在使用集合视图,一切进展顺利,我想在集合视图之外的文本字段结束编辑所有要更改的标签文本时添加功能。 这是我的密码 我尝试了newValue()函数的变体,但没有成功 import UIKit class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate, UITextFieldDelegate { var
这是我的密码 我尝试了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
}
}
我找到了很多答案,但没有一个能解决我的问题(你能分享你的代码吗?请更具体一点,用一句话很难解决你的问题谢谢你的关注,我已经更新了我的问题。)