jiren
jiren

Reputation: 17

Material pods ErrorTextField

Here is my code for the ErrorTextField in a sample project. It is working fine in a view controller but this this time I put it in a cell in a tableview. Now the error is not showing, worse the textFieldDidEndEditing is not triggering the saving of the date. Can anymore give pointers on how to properly implement this errortextfield in a tableview cell in ios? I am using Material pods 2.8 Please find the code below for reference.

import UIKit
import Material

class sampleTableViewController: UITableViewController, TextFieldDelegate {
var sampleData: Dictionary = [String: Any]()

override func viewDidLoad() {
    super.viewDidLoad()
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 2
}

public func textFieldDidEndEditing(_ textField: UITextField) {
    if textField.tag == 1 {
        self.saveValueFromTextField(key: "textValue", value: textField.text)
        print(self.sampleData)
        let indexPath = IndexPath(row: 1, section: 0)
        let cell = tableView.cellForRow(at: indexPath) as! sampleTableViewCell
        cell.sample.detail = "error"
    }
    (textField as? ErrorTextField)?.isErrorRevealed = false
}

public func textFieldShouldClear(_ textField: UITextField) -> Bool {
    (textField as? ErrorTextField)?.isErrorRevealed = false
    return true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    (textField as? ErrorTextField)?.isErrorRevealed = true
    return true
}

func saveValueFromTextField(key:String!, value:Any!){
    sampleData[key] = value
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! sampleTableViewCell

    cell.sample.text = "sample"

    return cell
    }
}

Here is the tableviewcell code:

import UIKit
import Material

class sampleTableViewCell: UITableViewCell {

@IBOutlet weak var sample: ErrorTextField!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

Upvotes: 0

Views: 244

Answers (0)

Related Questions