1

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

The text field whose value I have edited

As you can see, I have entered a numeric value to the text field that is in the 'Daycare' cell

The text field which has been edited unintentionally

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?

Pavan Vasan
  • 391
  • 1
  • 9
  • 28
  • 6
    This is a cell reuse issue. You need to maintain a proper data source and populate your text field in your cell with the data source every time it is dequeued. – Rakesha Shastri Dec 20 '18 at 09:49
  • @Rakesha Shastri: This issue, has it got something to do with the fact that I am having a UITableView inside my custom TableViewCell? – Pavan Vasan Dec 20 '18 at 10:20
  • 1
    It has to do with your Custom cell that you are reususing. Check out thread which explain cell reuse. These will help. https://stackoverflow.com/questions/52100899/uibutton-image-for-normal-state-in-collectionview-cell-repeats-itself-every-four/52102002#52102002, https://stackoverflow.com/questions/50916739/reusing-cells-in-uitableview/51152042#51152042 – Rakesha Shastri Dec 20 '18 at 10:23
  • @Rakesha Shastri: I looked into your links, it was exactly the same problem I was facing. Thank mate! :) – Pavan Vasan Dec 20 '18 at 11:38
  • I did upvote your answer mate. The one with the links – Pavan Vasan Dec 21 '18 at 06:31

1 Answers1

0

This problem arises when you dequeue your cell with the same identifier. To solve this problem, you have to save the values written in the cell in some data set and while reloading table view in cellForRow method, use that data set to configure your cell.

You can do that by making a delegate in your CustomCell and make your custom cell a delegate of the text field present in your cell and when text field did end editing you can pass those values to your controller via delegate and save those values in the datasource. While loading your tableview use that datasource to configure your cell

Habib Ali
  • 272
  • 2
  • 13