Multiple Row Selection in UIPickerView

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

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?

回答1:

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.



回答2:

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];     } }


回答3:

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!



回答4:

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.



回答5:

- (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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!