0

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
}

}
jiren
  • 7
  • 6
  • 2
    did you tried to set `cell.sample.delegate = self` in `cellForRowAt` method? – gorzki Jul 24 '18 at 16:12
  • Hi @MichalGorzalczany this solved the problem. Thank you. the problem now is its too far from the bottom of the textfield. Is there a way to programmatically adjust the distance of the cell.sample.detail from the textfield itself? Thanks again – jiren Jul 25 '18 at 04:25
  • Sorry @jiren, but im not familiar with this Material pod. Can't help you with that. – gorzki Jul 25 '18 at 08:46
  • 1
    You can adjust the vertical offset of the detail label using the `detailVerticalOffset` property. – CosmicMind Jul 26 '18 at 02:14

0 Answers0