使用文本字段更改 cell.label.text 在更改第二个 cell.label.text 后不会保存

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

我正在使用文本字段手动更改单元格标签的文本。文本变化很好,但是当我去更改另一个单元格的标签文本时,它会同时更改第一个和第二个标签的文本。我正在尝试使用它,以便在更改一个单元格的标签文本后将其保存,这样当另一个单元格的文本被更改时,它只会更改当前单元格的标签文本并保存它。

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

知道如何保存单元格的标签文本,以便在更改另一个单元格的标签文本时它不会改变吗?

您错过了 table 单元格被重新使用,因此您需要将编辑保存到模型然后重新加载 table,所以 delcare

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

然后用它编辑里面的当前indexPathchangeVitalValue

arr[indexPath] = vital

最后在cellForRowAt

里面赋值
cell.vitalTitleLabel.text = arr[indexPath.row] 

如果您已经有另一个数据源数组,那么考虑为 1 个数组创建一个结构