Swift选择时如何更改CollectionView UItextField背景色
大家好,我是Swift的新手,我在网上搜索了很长时间。但是没有用。请帮助或尝试给出一些如何实现这一点的想法 这是我什么都没做的第一张照片 第二张图片是当我已经选择了一个文本字段时 第三张图片是我选择所有Btn的时候 这四张图片是当我选择选择所有Btn并取消选择一个文本字段时显示的 现在我只能做像Pic二和Pic三,但我不能使Pic二文本字段textcolor当我选择 我怎么能像图四那样做呢 我正在使用collectionView这样做 我尝试了多种不同的方法,但似乎都不管用。有什么想法吗 这是我的代码Swift选择时如何更改CollectionView UItextField背景色,swift,uicollectionview,uitextfield,Swift,Uicollectionview,Uitextfield,大家好,我是Swift的新手,我在网上搜索了很长时间。但是没有用。请帮助或尝试给出一些如何实现这一点的想法 这是我什么都没做的第一张照片 第二张图片是当我已经选择了一个文本字段时 第三张图片是我选择所有Btn的时候 这四张图片是当我选择选择所有Btn并取消选择一个文本字段时显示的 现在我只能做像Pic二和Pic三,但我不能使Pic二文本字段textcolor当我选择 我怎么能像图四那样做呢 我正在使用collectionView这样做 我尝试了多种不同的方法,但似乎都不管用。有什么想法吗
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell
if isselectAll == true{
cell.titleText2.textColor = .white
cell.titleText2.backgroundColor = UIColor(red: 153/255, green: 31/255, blue: 37/255, alpha: 1)
selectedCell.append(indexPath)
}
return cell
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath)
{
let cell = collectionView.cellForItem(at: indexPath)!
selectedCell.append(indexPath)
cell.contentView.backgroundColor = UIColor(red: 153/255, green: 31/255, blue: 37/255, alpha: 1)
}
func collectionView(_ collectionView: UICollectionView,didDeselectItemAt indexPath: IndexPath)
{
let cell = collectionView.cellForItem(at: indexPath)!
if selectedCell.contains(indexPath) {
selectedCell.remove(at: selectedCell.index(of: indexPath)!)
cell.contentView.backgroundColor = .clear
}
想知道这样做是否是一种好的做法,最好的方法是什么?谢谢mate使用此代码(同时设置脚本中的文本字段委托)
因此,首先,在选择和取消选择时打印出indexath。检查项目是否按预期添加和删除。如果没有,那么这一步就有问题 如果数组值按预期更改,则意味着视图不会按您喜欢的方式更新,即使这些值已更改 现在,使用断点并检查您的预期代码位是否正在运行。如果是,则表示您编写的代码行是错误的。如果没有,这意味着你没有把它写在正确的地方 我可以通过运行代码给你一个简单的答案。但这并不能帮助你学会调试。按照我在上面说的步骤做,让我知道会发生什么 谢谢
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate {
@IBOutlet weak var collectedView: UICollectionView!
var flag = false
override func viewDidLoad() {
super.viewDidLoad()
self.collectedView.delegate = self
self.collectedView.dataSource = self
}
//press selected All Button
@IBAction func selcetedAll(_ sender: Any) {
self.flag = true
self.collectedView.reloadData()
}
//MARK: UIcolletionViewDelegate
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10 //return cell according to your requirement
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell
if flag {
cell.textF.backgroundColor = UIColor.red
}
cell.textF.text = String(indexPath.row)
return cell
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.backgroundColor == UIColor.red {
textField.backgroundColor = UIColor.white
} else {
textField.backgroundColor = UIColor.red
}
}
}