I want to select multiple rows in a UIPickerView, so I had the idea to show my data in a table and add this table as subview to the picker. I have tried this but didn't succeed.
Any suggestions how to do this?
I want to select multiple rows in a UIPickerView, so I had the idea to show my data in a table and add this table as subview to the picker. I have tried this but didn't succeed.
Any suggestions how to do this?
the "accepted" UI for multiple selection on the iPhone is to use a UITableView with checkmarks (i.e. don't use UIPickerView for multiple selection).
However, if you must, there are instructions on how to fake it here http://www.iphonedevsdk.com/forum/iphone-sdk-development/14634-uipickerview-multiple-selection.html by making an empty picker and putting a table view over the top of it.
You can do it without UITableView, using just UITapGestureRecognizer :)
Also, add NSMutableArray *selectedItems
somewhere in your .h file.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UITableViewCell *cell = (UITableViewCell *)view; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; [cell setBackgroundColor:[UIColor clearColor]]; [cell setBounds: CGRectMake(0, 0, cell.frame.size.width -20 , 44)]; UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleSelection:)]; singleTapGestureRecognizer.numberOfTapsRequired = 1; [cell addGestureRecognizer:singleTapGestureRecognizer]; } if ([selectedItems indexOfObject:[NSNumber numberWithInt:row]] != NSNotFound) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; } else { [cell setAccessoryType:UITableViewCellAccessoryNone]; } cell.textLabel.text = [datasource objectAtIndex:row]; cell.tag = row; return cell; } - (void)toggleSelection:(UITapGestureRecognizer *)recognizer { NSNumber *row = [NSNumber numberWithInt:recognizer.view.tag]; NSUInteger index = [selectedItems indexOfObject:row]; if (index != NSNotFound) { [selectedItems removeObjectAtIndex:index]; [(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryNone]; } else { [selectedItems addObject:row]; [(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryCheckmark]; } }
Implemented a quick hack to get the UIPickerView multiple-selection-behavior (like in Mobile Safari) without adding other views in front of the pickerview, if anyone's interested: https://github.com/alexleutgoeb/ALPickerView
Improvements are highly appreciated!
The following code works for iOS 10. I didn't have the chance to test it on older versions but I think it should work. The idea is similar to @suda's one but it adds a single tap recognizer directly to the picker view instead of adding the tap recognizer to each row, as this does not work on iOS7+.
For brevity, I did not include the complete implementation of the UIPickerViewDataSource
and UIPickerViewDelegate
protocols, only the relevant parts to implement the multiple selection.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return true; }
And you need to override this method,other else form ios9, tap gesture recognization wouldn't work.