I have a UIPickerView as subview in a UITableViewController, which I want to slide up in place when a certain row is clicked. I have implemented the necessary dataSource and delegate methods, but the pickerview acts weirdly. It acts as if it were mechanically jammed, it cannot rotate properly, it just moves a little. If I try to spin it a few times I get this message in the debugger console:
SendDelegateMessage: delegate failed to return after waiting 10 seconds. main run loop mode: UITrackingRunLoopMode If you were not using the touch screen for this entire interval (which can prolong this wait), please file a bug.
I googled for this, to no avail. Anyway, here is the relevant code. As you may guess, this is a number picker (to choose a percentage value).
- (void)viewDidLoad { [super viewDidLoad]; percentPicker = [[UIPickerViewalloc] initWithFrame:CGRectMake(0,420, 320, 200)]; percentPicker.delegate = self; percentPicker.dataSource = self; percentPicker.showsSelectionIndicator = YES; [self.view addSubview:percentPicker]; } - (void)viewWillAppear:(BOOL)animated { // sets the rows to the appropriate value // etc } - (void)startEditingPercentage { [UIView beginAnimations :nilcontext:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIViewsetAnimationDuration:kPickerAnimationDuration ]; percentPicker.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, -220.0); [UIViewcommitAnimations]; } - (void)stopEditingPercentage { NSLog(@'stopEditingPercentage'); [UIView beginAnimations :nilcontext:NULL]; [UIViewsetAnimationBeginsFromCurrentState:YES]; [UIViewsetAnimationDuration:kPickerAnimationDuration]; percentPicker.transform = CGAffineTransformIdentity; [UIView commitAnimations]; } #pragma mark UIPickerView delegate and dataSource methods - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return4; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return10; } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { return44; } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *retval = (UILabel *)view; if (!retval) { retval= [[UILabel newLabelWithPrimaryColor :[UIColorblackColor ] selectedColor:[ UIColor blackColor] fontSize:22bold:YEStransparent:YES] autorelease]; } retval.frame = CGRectMake(0, 0, 44, 44); retval.textAlignment = UITextAlignmentCenter; retval.backgroundColor = [UIColor clearColor]; retval.text = [NSStringstringWithFormat:@'%i', row]; if (component > 1 ) { // rows 2 and 3 are decimal, white on black retval.backgroundColor = [UIColor blackColor]; retval.textColor = [UIColor whiteColor]; } return retval; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSString *selectedValue; selectedValue= [NSString stringWithFormat:@'%i%i.%i%i' ,[percentPickerselectedRowInComponent :0], [ percentPickerselectedRowInComponent: 1], [percentPickerselectedRowInComponent:2], [percentPickerselectedRowInComponent:3]]; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; NSString *trimmedValue = [[formatter numberFromString:selectedValue] stringValue]; percentValue.text = trimmedValue; [formatter release]; }
As you see, I use the transform property to move the picker in and out (down) my main view; the startEditing and stopEditing methods are triggered by selection in the teble view. Yet, for the debugging purposes, I eliminated these transitions , and left the picker on top of the table, but nothing changed. I also commented the didSelect method, but this also didn’t change anything. By the way, the same picker-view construction code vorkw allright in another view of the same app. Any suggestion? Cheers,
You aren’t really adding the picker to UITableView using the code in viewDidLoad. UITableView can only have controls/rows in UITableViewCells and these are specified in the cellForRowAtIndex method. We cannot add controls to UITableView like we do to UIView.
Try using a subclass of UIView instead and add the UITableView and the picker to this UIView subclass.
In case you want to use a UITableViewController subclass only, create a custom cell and add the picker to this custom cell. But then you won’t be able to hide/unhide this cell if you add it statically. Then you’ll have to use reloadData. I would suggest using the UIView subclass.