Reuse the same pickerview for different textfields in tableview - ios

I want to display the pickerview as input for textfield in the tableview. I am reusing the same cell xib for all the rows in tableview. at indexpath.row == 1 want to change the age, so the input for textfield is pickerview containing numbers from 18 to 100. In the next row when click on textfield i need to change gender, so the data in picker view should be male and female. Please help me in achieving this.
Thanks in advance.

You can simply reload the picker view:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[Yourpickerview reloadAllComponents];
}

Set the tag of the textfield and in textFieldShouldBeginEditing get the tag and then add
pickerVw = [[UIPickerView alloc] init];
pickerVw.dataSource = self;
pickerVw.delegate = self;
pickerVw.tag = TAG_PICKER;
// ... ...
[pickerVw reloadAllComponents];

Please See the GitHub link Has demo made for the purpose in swift
Thanks

Related

Different picker data for every cell

I have a table view with custom cells, each cell contains a textField. When the user tap on the textfield a UIPickerView will be displayed. I have this approach already done and work good. The thing is, the picker should display different data depending on which textField has been tapped, how can I detect this?
What I've done:
In viewDidLoad I create a view property which contains a UIPickerView.
In cellForRowAtIndexPath: I assign that view with the picker as an inputView for the textField (I did it this way to avoid creating a picker everytime a cell is rendered)
Ok, I found a solution that works really smooth:
I disabled the textField on every cell
I stop adding the picker as an inputView in cellForRowAtIndexPath
Once the user tap on didSelectRowAtIndexPath (which is called when they tap on the textField) I do the following:
if ([dataInfo[indexPath.row] isEqualToString:#"Accessibility"]) {
pickerItems = accessibilityItems;
}
else if ([dataInfo[indexPath.row] isEqualToString:#"Floor"]){
pickerItems = floorItems;
}
[picker reloadAllComponents];
SubCellTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.subcellSubtitleTextField setEnabled:YES];
cell.subcellSubtitleTextField.inputView = viewContainingPicker;
[cell.subcellSubtitleTextField becomeFirstResponder];
I hope this can help someone else!

How to implement UIPickerView for the textfield in for loop?

I am displaying text fields in a UIView dynamically, based on JSON response data using a for loop.
I need to show a UIPickerView for the each text field in the for loop, but it is only working for the last text field. I am unable to display a UIPickerView for each of the text fields. Can anyone please help me to solve this issue?
So you are inside your for loop with an i index. What we are looking here is to assign a UIPickerView as each textfield's input view. Try something like this...
//FOR LOOP BEGINS
//Iteration - Create a textfield.
[self.view addSubview:yourTextfield];
//Assign a uipickerview as textfield's input view.
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.frame = CGRectMake(0,0,300,300);
pickerView.tag = i;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
textField.inputView = pickerView;
[self.view addSubview:pickerView];
//Iteration ends
//FOR LOOP ENDS
P.S. You can also add a toolbar with each pickerView to your textfield using the inputAccessoryView
You can set the number of rows to a large number, and make it start at a high value, there's little chance that the user will ever scroll the wheel for a very long time -- And even then, the worse that will happen is that they'll hit the bottom.
you can see this:
How do you make an UIPickerView component wrap around?

UIToolbar not responding in Custom UITableViewCell with UIPickerView

I've searched this for a while but can't find anything quite the same.
I have a UITableView, and when a certain row is selected, I insert another row below.
The inserted row is a custom tableviewcell which hold a UIPickerView.
The pickerview works fine, and when an item is selected it can trigger the notification, sending selected info back to the tableviewcontroller, and then remove the "pickerviewcell". All good there.
But this isn't ideal if the user wants to scroll back and forth on the uipickerview. So I've added a uitoolbar to the uipickerview with a Cancel & Done button.
But the Cancel and Done buttons never get fired.
From other items I have read, they talk about UIFirstResponder etc etc, but they are all related to making the uipickerview an inputaccessoryview for a uitextfield. But that is not what I am doing.
I've tried doing it all in code and via Storyboards, with the same results each time.
Some example below..
// (in my CustomTableViewCell's AwakeFromNib function)
let screenSize: CGRect = UIScreen.mainScreen().bounds
pickerView = UIPickerView(frame: CGRectMake(0,0, screenSize.width, 162))
pickerView.delegate = self
pickerView.dataSource = self
pickerToolbar.barStyle = UIBarStyle.Default
pickerToolbar.translucent = true
pickerToolbar.tintColor = UIColor.orangeColor()
pickerToolbar.sizeToFit()
pickerToolbar.userInteractionEnabled = true
pickerView.addSubview(pickerToolbar)
self.contentView.insertSubview(pickerView, atIndex: 3)
// both these logs show correct output
NSLog("picker subviews: %#", pickerView.subviews.description)
NSLog("toolbar subviews: %#", pickerToolbar.subviews.description)
Screenshot example:
By clicking on the "To" cell, the new cell is inserted which has the picker. The picker works fine by itself. But the Cancel button doesn't get triggered. It has an IBAction linked to it from Storyboard.
Clicking any cell also closes/removes the pickercell correctly.
I have made a picker within a UITextview and I using
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return NO;
and then dismissing the keyboard with
-(void)dismissKeyboard {
[_date resignFirstResponder];
}
where _date was my UITextfield property
Dismissing UIPickerView with Done button on UIToolBar
this is a really good post I found on this topic as well! I hope it helped.

Two clickable items in UITableView

I have a UITableview as a contact list in which there are a lot of users. It has a thumbnail photo and profile details on each row. I want to make it like when clicking on thumbnail, it goes another page for photo and when clicking on the rest of the space it goes to somewhere else. By using table view delegate I know which row is clicked and pass data, like user id to a new ViewController. But can I know which row when the thumbnail is clicked?
I am using the tag to find the view from cell, like
UIImageView *thumbnailView = (UIImageView *) [cell viewWithTag:1];
I think I cannot label the row index by tag.
You can add gesture to thumbnail image and get events on it. Need to set tag for thumb image as per indexPath.row.
Add following code in you in cell datasource method (cellForRowIndexPath:) :
cell.YOURIMGVIEW.userInteractionEnabled = YES;
cell.YOURIMGVIEW.tag = indexPath.row;
UITapGestureRecognizer *clickable = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageClicked:)];
clickable.numberOfTapsRequired = 1;
[cell.YOURIMGVIEW addGestureRecognizer:clickable];
[clickable release];
And also used below method :
-(void)imageClicked:(id)sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(#"image tag is = %d", gesture.view.tag);
////////
You custome come goes Here !!!!!!!!!!!!!!
///////
}
You can use the photo as accessory view. Or use a UIButton with the photograph as background and set the action accordingly. (e.g. call a method on the view controller which then performs the push or segue. Doing so you will have to pass some data to the view controller indicating which row was actually clicked in. The number of the row can be used or you can set the tag of the button with some numeric id.)
Go the Easy way because doing it hard-way won't grant you a president award, right ?
Instead of UIImageView take a UIButton and set the Image on UIButton instance.
Set Button tag as the indexPath.row so that when you retrieve it you know which row is clicked. You can also sort it the other way but it seems quiet handy.
Add target into the button to a custom function. [ btnObj addTarget ...... ]
tyepcast you sender to a UIButton and receive the tag (indexpath.row)
You can remove all gesture recognizers and buttons in tableviewcell, and in your controller, you can implement below delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
To make this method triggered, you need to set tableview delegate as your controller, either via code as below, or using storyboard.
- (void)viewDidLoad {
self.tableview.delegate = self;
}
By doing this, it won't matter which item you clicked in your cell, delegate method will be called always.
I hope this helps.

picker wheel that only appears when selected

I have a view controller that displays various UITextfields to edit information. For one UITextfield I need a picker wheel to select predefined statuses.
Problem is I don't have enough space to integrate a picker wheel, is there a possibility to make it appear only when the text box is selected?
You could set the UIPickerView as the UITextField's inputView.
This will ensure that the picker is shown automatically when the text field gets focus, and hides it when lost.
E.g.
myTextField.inputView = self.myPickerView;
See the documentation on this property.
Assuming your "picker wheel" is a UIView, just hook up your controller as the UITextField's delegate and implement the following:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.pickerWheel.hidden = NO;
}
You need to call your UIPicker in (BOOL)textFieldShouldBeginEditing:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==self.yourtextfield) {
//call your wheel in here
}
}
Look at
How to Show UIPickerView when selecting UITextField

Resources