Setting UIPickerView selection to UITextField inside a UITableViewCell
I have a UITextField in a custom tableview cell. On tapping the textfield area I display a popover which contains a pickerview. I need to set the selection from the pickerview to the textfield's text. I show the popover from the textDidBeginEditing method. How do I pass the selection from the pickerVie开发者_如何转开发w's didSelect method to the textfield?
By design the entire thing is in a TableViewController with the textfield and pickerview delegate methods implemented.
Thanks!
Just set the UIPickerView's delegate to be the viewcontroller that has your UITableView. That way on the UIPickerView's didSelect (implemented in the tableview's viewcontroller) you would simply use the pickerview's datasource to populate the textfield.
To get your custom cell from a tableview you simply do:
CustomTableViewCell *thisCell = (CustomTableViewCell*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
Then you set the textfield in that to have whatever text you want:
thisCell.textField.text = @"text";
Can you provide a little more information? What's the tableView:cellForRowAtIndexPath:
look like? I'm curious where the object you're editing is available.
There are a couple approaches I can think of...
- Set the tag on the textField(s) and use it to locate the specific textField being edited
- Set the delegate for the pickerView to the tableCell and have it handle the textual change
I believe the best approach is to set the UITextField's inputView
property to a UIPickerView. This view will be displayed instead of the keyboard.
When a UITableViewCell is scrolled beyond a table's bounds, it may be deallocated. Therefore, storing a reference to the UITextField which invoked its textFieldDidBeginEditing:
delegate method could result in an EXC_BAD_ACCESS
exception.
The solution is to modify the cell's model which is stored in an array by the view controller. When the cell is initialized, it should set the text field's value to the corresponding value in its model object. Finally, the cell should observe changes to its model via KVO and update the value of the text field accordingly.
精彩评论