Search code examples
swiftuitableviewdelegatesuitextfieldprotocols

Using a textfield to change cell.label.text doesn't save after changing second cell.label.text


I'm using a textfield to manually change the text of a cell label. The text changes fine but when I go to change another cell's label text it changes both the first and second labels' text. I'm trying to have it so after a cell's label text is changed it saves it so when another cell's text is being changed it only changes that CURRENT cell's label text and saves it.

protocol ChangeVitalsViewControllerDelegate {
    func changeVitalValue(vital: String, indexPath: Int)
}

protocol VitalViewControllerDelegate {
    func selected()
}

class VitalCell: UICollectionViewCell {

    @IBOutlet weak var vitalTitleLabel: UILabel!

    var vitalDelegate: VitalViewControllerDelegate!
    var buttonAction: ((Any) -> Void)?

    @IBAction func infoButtonPressed(_ sender: Any) {
        self.buttonAction?(sender)
        vitalDelegate.selected()
    }
}

class ManualVitalsInputViewController: UIViewController {

    var delegate: ChangeVitalsViewControllerDelegate!
    var currentIndex: Int!

    @IBAction func saveButtonTapped(_ sender: Any) {
        delegate.changeVitalValue(vital: enterInfoTextfield.text!, indexPath: currentIndex)
        dismiss(animated: true)
    }

}

class VitalsViewController: UIViewController{

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let vital = vitals[indexPath.row]
        cell.imageView.image = vital.image

        if indexPath.row == changedVitalIndexPath && changedManualVital != nil {
            cell.vitalTitleLabel.text = changedManualVital
        } else if manuallyChangedVitalsArrayIndexs.contains(indexPath.row){
            cell.vitalTitleLabel.text = changedManualVital//pretty sure this is issue but not sure what else to put here
        } else {
            cell.vitalTitleLabel.text = vital.title
        }

        cell.vitalDelegate = self

        cell.buttonAction = { sender in
            self.currentIndexPath = indexPath.row
            self.manuallyChangedVitalsArrayIndexs.append(self.currentIndexPath!)
        }
    }
}

extension VitalsViewController: VitalViewControllerDelegate {

    func selected() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "ManualVitalsInputViewController") as! ManualVitalsInputViewController
        vc.delegate = self
        vc.currentIndex = currentIndexPath

        present(vc, animated: false)
    }
}

extension VitalsViewController: ChangeVitalsViewControllerDelegate {

    func changeVitalValue(vital: String, indexPath: Int) {
        self.changedManualVital = vital
        self.changedVitalIndexPath = indexPath

        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)// this is used to reload collectionview
    }
}

Any idea on how to save cell's label text so it doesn't change when another cell's label text is being changed?


Solution

  • You miss that table cells are re-used , so you need to save edits to a model and then reload the table , so delcare

    var arr = [String](repeating: "", count: numberOfRows)
    

    Then use it to edit the current indexPath inside changeVitalValue

    arr[indexPath] = vital
    

    last assign it inside cellForRowAt

    cell.vitalTitleLabel.text = arr[indexPath.row] 
    

    if you already have another dataSource array , then consider creating a struct for 1 array sake