更改一个文本字段的值也会无意中更改其他两个文本字段。我该如何预防?

时间:2018-12-20 09:44:49

标签: ios swift uitableview uitextfield

我有一个自定义表格视图单元格的表格视图。每个单元格都有一个文本字段,我可以在其中填写一些数字数据。我设置了一个委托方法,即textFieldDidEndEditing,该方法在编辑后会将输入到文本字段中的值添加到快速哈希表中。

但是,我看到属于另一个完全不同的表视图单元格的其他一些文本字段现在也具有与我输入的相同的值。

为了解决此问题,我尝试添加其他基于委托的文本字段方法,认为它们应该解决当前的问题。我使用的一种方法是textFieldDidChange方法,在该方法中,我编写了检查以确保文本字段标签与故意编辑的文本字段的标签不同,然后将文本字段清除。

func textFieldDidChange(_ textField: UITextField) {
    if textField.tag != self.service!.id {
        print("CLEARING AFFECTED TEXTFIELD")
        textField.text = ""
    }
}

我可能以错误的方式使用了该方法,因为它对问题没有任何影响。

我要添加与当前问题有关的代码段:-

BookingServiceChargeViewCell.swift     导入UIKit     导入PineKit     导入SwiftMoment

class BookingServiceChargeViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

    //Other variables
    let price = TextField(placeholder: "Price")

    //Other methods
    func layoutContent() {
        //function to set the layout of the cell
        self.price.font = Config.Font.get(.Regular, size: 13)
        self.price.delegate = self
        self.price.setValue(UIColor.init(colorLiteralRed: 71/255, green: 72/255, blue: 73/255, alpha: 1.0), forKeyPath: "_placeholderLabel.textColor")
        self.price.keyboardType = UIKeyboardType.numberPad
        self.price.addDoneButtonOnKeyboard()
        self.price.setBottomBorder()
        self.price.snp.makeConstraints { (make) in
            make.centerY.equalTo(cover)
            make.width.equalTo(75)
            make.right.equalTo(content.snp.right).offset(-40)
        }
    }

    func configure(_ service: Service, subServices: [Service], index: Int, parentView: OnboardingChosenServicesViewController) {
        self.service = service
        self.subServices = subServices
        self.itemIndex = index
        self.parentView = parentView

        if (self.service!.defaultImage != nil){
            ImageLoader.sharedLoader.imageForUrl(urlString: self.service!.defaultImage!) { (image, url) in
                self.cover.image = image
            }
        }

        self.serviceName.text = self.service!.name!
        self.price.tag = self.service!.id
        self.table.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.subServices.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! BookingSubServicesChargeViewCell
        cell.configure(self.subServices[indexPath.row], index: indexPath.row, parentView: self.parentView!)
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        self.parentView!.serviceAndCharges[textField.tag] = Int(textField.text!)
        print(self.parentView!.serviceAndCharges)
    }
}

我正在上传一些显示问题的屏幕截图:-

The text field whose value I have edited

如您所见,我已经在“日托”单元格的文本字段中输入了一个数字值

The text field which has been edited unintentionally

在此屏幕截图中,“行走”单元格中的文本字段根本不应该被编辑,除了我刚刚编辑的文本字段之外,它应该与其他所有文本字段一样保持空白。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

当您使用相同的标识符将单元出队时,就会出现此问题。要解决此问题,您必须将写在单元格中的值保存在某些数据集中,并在cellForRow方法中重新加载表视图时,使用该数据集来配置您的单元格。

您可以通过在CustomCell中创建一个委托并将您的自定义单元格作为您单元格中存在的文本字段的委托来实现,当文本字段结束编辑时,您可以通过委托将这些值传递给控制器​​并保存这些值在数据源中。加载表格视图时,使用该数据源配置单元格