Getting value text field from form based UITableView

I got a UITableView with one custom cell that will show the question data from the json server response. So, I got 1 UILabel, 3 UIButtons (which will be a switch) and 1 UITextField, which I declare like this.

enter image description here

Below the text box in each cell of the table is a note, and the switch (Yes, No, N / A) is only UIButtons.

These are polling questions, as you can see, set this to a UITableView.

If the server has the answer "21 questions", I will show 21 lines in which the name of the question, selection buttons and a note on each table cell will be displayed.

So what is my problem, I really don't know how to get data from each cell of the table when the user clicks the submit button.

When I send, I want to receive such data: [[String: String], [String: String], ...]

Example. Suppose that I am examining the 1st and 2nd cell of the table. I want to get such data. You will see that the third is empty data if the user has not filled in.

[ ["QuestionID": 1,"Type":"Yes","Remark":"Nothing special everything good"], ["QuestionID": 2,"Type":"No","Remark":"Yeah,it will be alot helpful"], ["QuestionID": 3,"Type":"","Remark":""] ] 

Any help? Please, how to get the data and save it, even if the user did not fill out when we send the survey.

+1
uitableview swift
source share
1 answer

You can define specific UITextField or UIButton by getting NSIndexPath . You just need to addTarget and get the current NSIndexPath , and from there you can get the current click of UITableViewCell .

First addTarget in a UITextField in cellForRowAtIndexPath :

 cell.textFieldSearch.addTarget(self, action: "textChange:", forControlEvents: UIControlEvents.EditingChanged) 

Then you can handle the click event in the following function:

 func textChange(textField: UITextField){ var cellIndexPath = tableViewSearch.indexPathForCell(textField.superview!.superview! as! UITableViewCell)! let cell = tableViewSearch.cellForRowAtIndexPath(cellIndexPath!) as! SearchRatingCell cell.textFieldSearch.text = textField.text! } 

Update ( UIButton ):

 cell.yesButton.addTarget(self, action: "searchByRating:", forControlEvents: UIControlEvents.TouchUpInside) 

Custom method:

 func searchByRating(sender: UIButton){ let cellIndexPath = tableViewSearch.indexPathForCell(sender.superview!.superview! as! SearchRatingCell)! let cell = tableViewSearch.cellForRowAtIndexPath(cellIndexPath!) as! SearchRatingCell cell.yesButton.selected = true } 

Let me know if you have any confusion.

+2
source share

All Articles