Vladislav Yarmak
Vladislav Yarmak

Reputation: 125

How to update text of labels in collection view?

I am working with collection view, all is going well and I want to add function when text field ,outside of collection view, end editing all labels text to be changed. Here is my code

I tried my variation of newValue() function but no luck.

   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
    }
  
    

}

What function should I add to update all text by save button.? Thank you)!

Upvotes: 1

Views: 116

Answers (1)

Shivam Parmar
Shivam Parmar

Reputation: 1570

  • add all changed values in array (text2 in your code)
  • once you update values call this method self.collectionview.reloadData()

[edited]

    func reload(){
        self.text2 = ["new data1", "newd ata2" , "new data3"]
        self.collectionView.reloadData()
    }

Upvotes: 1

Related Questions