Irwan Madness
Irwan Madness

Reputation: 1396

How to get index uitextfiled on UITableViewCell with Swift

I'm trying to retrieve the Text Field value from UITableViewCell index and update to realm. code :

 func textFieldDidBeginEditing(_ textField: UITextField) {
    let index = IndexPath(row: 0, section: 0)
    let cell: OthersTableViewCell = self.tableView.cellForRow(at: index) as! OthersTableViewCell
    self.firstnameOther = cell.firstNameTxt.text!
    self.lastnameOther = cell.lastNameTxt.text!
    self.countId = index.row
    print("self.countId\(self.countId)")
    try! realm.write {
        realm.create(Others.self, value: ["id": self.id, "firstname": self.firstnameOther!, "lastname": self.lastnameOther!], update: true)
    }

    others = realm.objects(Others.self)
    print(others)
}

i need to input in textfield on uitabelview by index row

Upvotes: 0

Views: 1319

Answers (1)

Jaydeep Vora
Jaydeep Vora

Reputation: 6213

Set tag to UITextField like as below in UITableView datasource method

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        //Create you UITableViewCell Object and do your stuff then set tag to firstNameTxt by indexPath.row

         cell.firstNameTxt.tag = indexPath.row
    }

and use it like below

func textFieldDidBeginEditing(_ textField: UITextField) {
    let index = IndexPath(row: textField.tag, section: 0)
    let cell: OthersTableViewCell = self.tableView.cellForRow(at: index) as! OthersTableViewCell

   //Do your stuff
  }

Upvotes: 4

Related Questions