首页 > 解决方案 > 如何更新集合视图中的标签文本?

问题描述

我正在使用集合视图,一切顺利,我想在集合视图之外的文本字段结束编辑所有要更改的标签文本时添加功能。这是我的代码

我尝试了 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
    }
  
    

}

我应该添加什么功能来通过保存按钮更新所有文本。?谢谢)!

标签: swifttextuicollectionviewlabel

解决方案


  • 在数组中添加所有更改的值(代码中的 text2)
  • 一旦你更新值调用这个方法 self.collectionview.reloadData()

[编辑]

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

推荐阅读