Reputation: 1253
I want to implement in-line UIPickerView in my app but not find the good code to achieve this, it looks like little bit complicated.
Is there any good Example or Tutorial for add in-line UIPickerview in my iOS app?
Answer will greatly appreciated
Thanks
Upvotes: 0
Views: 414
Reputation: 1335
Basic understanding of UIPickerView:
You need to have these methods, pretty much like UITableView:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component <br>
You can populate picker values via array, for example, in viewDidLoad:
NSArray *values = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];
Upvotes: 0
Reputation: 2041
I like this tutorial, which shows how to do it via static cells and via dynamic cells: http://ios-blog.co.uk/tutorials/ios-7-in-line-uidatepicker-part-1/
The gist is, make a second cell with height 0. When the user "expands" the row you want, flip a bool that indicates "picker view open" and reload that cell, now returning the new height for that picker view row.
Just as a note to those who asked "what does this mean", the reference is to the new iOS picker style where instead of sliding up from the bottom of the screen, the picker is shown just below the field to be edited. Often this is implemented in a tableview where one cell is the field to be edited, and when the user touches that field, the cell expands, revealing a picker right below it. An example of this is in the native Settings app, in General --> Date & Time, if you turn off Set Automatically, you'll see the current date and time. Then if you select that row, a date picker appears right below it.
Upvotes: 1
Reputation: 1438
A quick and dirty method
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect frame = CGRectMake(CGRectGetMinX(cell.frame) - tableView.contentOffset.x, CGRectGetMaxY(cell.frame) - tableView.contentOffset.y, CGRectGetWidth(cell.frame), CGRectGetHeight(self.view.frame) - tableView.contentOffset.y);
UIView *snapShot = [self.view resizableSnapshotViewFromRect:frame afterScreenUpdates:NO withCapInsets:UIEdgeInsetsZero];
UIPickerView *picker = [[UIPickerView alloc]initWithFrame:frame];
picker.dataSource = self;
picker.delegate = self;
snapShot.frame = frame;
[self.view addSubview:snapShot];
picker.backgroundColor = [UIColor whiteColor];
[self.view addSubview:picker];
[self.view insertSubview:snapShot aboveSubview:picker];
[UIView animateWithDuration:2 animations:^{
snapShot.frame = CGRectOffset(snapShot.frame, 0, CGRectGetHeight(picker.frame));
}];
Upvotes: 0