Ios UICollectionView-如何使用一个单元格的更改更新所有单元格
我有一个Ios UICollectionView-如何使用一个单元格的更改更新所有单元格,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有一个CollectionView,在这个CollectionView的每个单元格中都有一个textField 如果textfield的所有内容都是nil,当我更改一个textfield的内容时(例如cell_1),所有其他textfield将自动更新具有值的内容,就像cell_1中textfield的内容一样 我已尝试使用collectionView.visibleCells,但它在我的场景中无法正常工作 是否有任何方法可以更改一个单元格,然后更新CollectionView中的所有单元格
CollectionView
,在这个CollectionView
的每个单元格中都有一个textField
如果textfield
的所有内容都是nil
,当我更改一个textfield
的内容时(例如cell_1),所有其他textfield
将自动更新具有值的内容,就像cell_1中textfield
的内容一样
我已尝试使用collectionView.visibleCells
,但它在我的场景中无法正常工作
是否有任何方法可以更改一个单元格,然后更新CollectionView
中的所有单元格
请帮忙。提前谢谢 清除新单元格:
由于我对你的问题没有100%的理解,我假设你的问题是新的单元格得到了cell_1的值,这不是你想要的 如果是这种情况,则在
UICollectionViewCell
中有一个名为prepareforeuse
的函数。
在UICollectionViewCell
的子类中,实现以下功能:
override func prepareForReuse() {
super.prepareForReuse()
textfield.text = ""
}
考虑到您的textfield
被称为textfield
,这应该可以做到这一点
执行任何必要的清理,以准备再次使用视图。
更新所有单元格:如果您的问题是如何更新其他单元格以获得与单元格1相同的内容,则这意味着集合视图子类需要了解单元格1中的更新,然后将该更新转换为其他单元格。 一种方法是在集合视图中设置
textfield
s委托。当值发生更改(textField(uu:shouldChangeCharactersIn:replacementString:)
)时,获取所有可见单元格并相应地更新这些textField
。也可以保存对单元格_1中文本字段的引用,或者在集合视图中保存一个字符串变量,说明最新输入的值,以便在渲染单元格时更新单元格。
您不想使用reloadData()
,因为这样会从textField
中移除焦点,因为textField
被重新加载
解决方案:
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
fileprivate var customString: String?
}
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.textField.delegate = self
cell.textField.text = customString
return cell
}
}
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
customString = string
for cell in collectionView.visibleCells {
if let cell = cell as? CustomCell,
let text = textField.text,
cell.textField != textField {
cell.textField.text = text+string
}
}
return true
}
}
上面代码中唯一的缺点是它在当前可见的单元格中循环。通常,除非绝对必要,否则我建议不要使用循环,但上述解决方案的替代方案是发布通知,让单元格侦听该通知并相应地更新其内容。这样做不仅是过分的,而且是通知并不真正适用的。第三种选择是在数组中保留对每个文本字段的弱引用,这可能不是最优的,因为它会产生不必要的开销。毕竟,visibleCells
已经是一个当前可见的单元格数组,每个单元格都包含对UITextField
的引用 清除新单元格:
由于我对你的问题没有100%的理解,我假设你的问题是新的单元格得到了cell_1的值,这不是你想要的
如果是这种情况,则在UICollectionViewCell
中有一个名为prepareforeuse
的函数。
在UICollectionViewCell
的子类中,实现以下功能:
override func prepareForReuse() {
super.prepareForReuse()
textfield.text = ""
}
考虑到您的textfield
被称为textfield
,这应该可以做到这一点
执行任何必要的清理,以准备再次使用视图。
更新所有单元格:
如果您的问题是如何更新其他单元格以获得与单元格1相同的内容,则这意味着集合视图子类需要了解单元格1中的更新,然后将该更新转换为其他单元格。
一种方法是在集合视图中设置textfield
s委托。当值发生更改(textField(uu:shouldChangeCharactersIn:replacementString:)
)时,获取所有可见单元格并相应地更新这些textField
。也可以保存对单元格_1中文本字段的引用,或者在集合视图中保存一个字符串变量,说明最新输入的值,以便在渲染单元格时更新单元格。
您不想使用reloadData()
,因为这样会从textField
中移除焦点,因为textField
被重新加载
解决方案:
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
fileprivate var customString: String?
}
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.textField.delegate = self
cell.textField.text = customString
return cell
}
}
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
customString = string
for cell in collectionView.visibleCells {
if let cell = cell as? CustomCell,
let text = textField.text,
cell.textField != textField {
cell.textField.text = text+string
}
}
return true
}
}
上面代码中唯一的缺点是它在当前可见的单元格中循环。通常,除非绝对必要,否则我建议不要使用循环,但上述解决方案的替代方案是发布通知,让单元格侦听该通知并相应地更新其内容。这样做不仅是过分的,而且是通知并不真正适用的。第三种选择是在数组中保留对每个文本字段的弱引用,这可能不是最优的,因为它会产生不必要的开销。毕竟,visibleCells
已经是一个当前可见的单元格数组,每个单元格都包含对UITextField
的引用 试试这个-
第一步-
创建集合视图的变量,如和-
var xyzString: String?
@IBOutlet weak var collectionView: UICollectionView! // IBOutlet of your collection view
步骤2-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TagViewCell", for: indexPath) as? TagViewCell // instead of TagViewCell give your custom cell class name
if self.xyzString != nil {
cell?.txtField.text = self.xyzString // if you want same value for all cell
}
return cell!
}
步骤3-
func textFieldDidEndEditing(_ textField: UITextField) {
self.xyzString = textField.text
self.collectionView.reloadData()
}
试试这个-
第一步-
创建集合视图的变量,如和-
var xyzString: String?
@IBOutlet weak var collectionView: UICollectionView! // IBOutlet of your collection view
步骤2-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TagViewCell", for: indexPath) as? TagViewCell // instead of TagViewCell give your custom cell class name
if self.xyzString != nil {
cell?.txtField.text = self.xyzString // if you want same value for all cell
}
return cell!
}
步骤3-
func textFieldDidEndEditing(_ textField: UITextField) {
self.xyzString = textField.text
self.collectionView.reloadData()
}
我没有完全明白你的意思,是你的问题是所有单元格都获得单元格_1的值,还是你希望所有单元格在单元格_1的textfield
中输入内容后立即更新为单元格_1的值?@PaulPeelen我希望所有单元格