I have a table view of custom table view cells. Each of the cells has a text field where I can fill in some numeric data. I have set up a delegate method i.e. textFieldDidEndEditing
which after editing will add the value entered into a text field into a swift hashtable.
However, I see that some other text field which belongs to another completely different table view cell also now has the same value that I entered.
In order to solve this problem, I tried to add other delegate based text field methods thinking that they should solve the problem at hand. One of the methods I used was the textFieldDidChange method and in that method, I wrote the check that if the text field tag was not the same as the tag of the deliberately edited text field, then I clear the text field out.
func textFieldDidChange(_ textField: UITextField) {
if textField.tag != self.service!.id {
print("CLEARING AFFECTED TEXTFIELD")
textField.text = ""
}
}
I probably used the method the wrong way as it did not have any effect on the problem.
I am adding the code snippets which are involved in the problem at hand:-
BookingServiceChargeViewCell.swift import UIKit import PineKit import 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)
}
}
I am uploading a couple of screenshots showing the problem:-
As you can see, I have entered a numeric value to the text field that is in the 'Daycare' cell
In this screenshot the text field in the 'Walking' cell should not have been edited at all, it should have remained blank like all other text fields except the one I just edited.
How can I solve this problem?