Jeff
Jeff

Reputation: 119

Swift how to change CollectionView UItextField backgroundColor when Select

hi guys I'm new in Swift and I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

this first pic when I didn't do anything

enter image description here

The Second Pic is when I already select one textfield

enter image description here

The Third Pic is when I selected that selected All Btn

enter image description here

The Four Pic is when I selected that select All Btn and Deselect one textfield

enter image description here

now I only can do like Pic two and Pic Third but I can't make the Pic two textField textcolor when I selected.

How can I do that like Pic Four?

I'm use collectionView to do like this Ive tried multiple variations of this, but none of them seem to work. Any ideas?

here is my code

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
       }

Wanted to know if it's good practice to do that and what would be the best way to do that?thanks mate

Upvotes: 1

Views: 696

Answers (2)

Clinton D'Souza
Clinton D'Souza

Reputation: 301

So first, print out the indexPath's as you select and deselect. Check if the items are being added and removed as expected. If not, there is something wrong with that step.

If the array values are changing as expected, it means the views are not updating as you like it even thought the values are.

Now, use breakpoints and check if your bits of expected code is running. If it is, it means thats the line of code you've written is wrong. If not, it means that you haven't written it in the right place.

I can give you a simple answer by just running your code. But it doesn't help you learn to debug. Do the steps I said on top and let me know what happens.

Thanks.

Upvotes: 0

shalu tyagi
shalu tyagi

Reputation: 148

use this code (set also text field Delegate from storyboard)

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
}
}
}

Upvotes: 1

Related Questions