-
-
Save odemolliens/7538173412e3c84eb6edcf684a628417 to your computer and use it in GitHub Desktop.
Multiple Row Selection in UIPickerView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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)]; | |
cell.tag = row; | |
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]; | |
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]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment