1

i have a custom uitableviewcell with a label and a textfield now i want to pass the data from the textfield when it is changed to the uitableviewcontroller.

The tableview is populated with data from a sqlite database that is wrapped with fmdb and the number of tableviewcells is different in every db i use in the app. How many fields there are and what name they have is stored in the db, the population of the table works fine.

I managed to get a "EditingDidEnd" event from the textfield in the cells to my view controller, the event is used to activate a toolbar button for saving changes. I did this via the cell.fieldData.addTarget(self, action: "SaveButtonOn:", forControlEvents: UIControlEvents.EditingDidEnd)

on creating the Cells in

tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath).

My problem is now that i need to get the data from the textfields inside the cells. I have found some code and tutorials for passing parameters to the via addTarget() but only for buttons and i don't know if that is usable for textfields.

If you need more information just ask, i hope you can decipher what i need to do.

Thanks for any answers Adarkas

2 Answers 2

1

Use delegation with your UITextField:

    import UIKit
    class MyViewController: UIViewController, UITextFieldDelegate {
      var myTextField: UITextField!

        override func viewDidLoad() {
            super.viewDidLoad()
            myTextField = UITextField(frame: CGRectMake(0, 0, 100, 30))
            myTextField.delegate = self
        }

      func textFieldDidEndEditing(textField: UITextField) {
        // Do stuff here.
      }
    }

If properly set, any textField you have calls the delegate-method textFieldDidEndEditing and from there on you can do with its content (textField.text) what you want.

5
  • Wow thanks that was way easier then i imagined. Can i get the text of the label in the tableviewcell somehow in that function? Because i need it to write the chanded text in the right column of the db. Commented May 18, 2015 at 10:39
  • Use a corresponding tag for your label to find it. Commented May 18, 2015 at 10:54
  • By corresponding do you mean for example the row number because i don't get the row number when the delegate is called. Or do i? Commented May 18, 2015 at 11:00
  • You can add a Tag to the textFIeld (which is passed by the delegate call) which implements the selectedRow indexpath or sth like that. Then you can determine the corresponding label. Or maybe work with the .leftViewproperty Commented May 18, 2015 at 11:57
  • Thanks i kind of did a combination of your answer and the one from @Vizllx so i did that already and it works just how i wanted it to. So thanks again. Commented May 18, 2015 at 12:05
1

To get value from a UITextFeild in a specific cell of UITableView:-

  1. Make sure you set a tag for your UITextFeild first, it's better you set tag with indexpath.row like cell.textFld.tag=indexpath.row
  2. Now write the following code to retrieve value from textfeild:-

let indexPath = NSIndexPath(forRow:row, inSection:0) //just provide the row number from where you want to fetch the textfeild

let cell : UITableViewCell? = self.tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell?

(cell.contentView.viewWithTag(2) as UITextFeild).text // access the value of textfeild by its tag with this way
1
  • Hm that would be better but how do i get the right row number, where the user did some editing in my TableView? Commented May 18, 2015 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.