Skip to content

Instantly share code, notes, and snippets.

@suda
Last active June 14, 2019 09:06
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save suda/4722708 to your computer and use it in GitHub Desktop.
Multiple Row Selection in UIPickerView
- (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.tab = 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];
}
}
@08irfan
Copy link

08irfan commented Jun 12, 2019

Hi Suda :)
Hope you are doing well.
Had a question related to this code..

@suda
Copy link
Author

suda commented Jun 13, 2019

@08irfan sure! What is your question?

@08irfan
Copy link

08irfan commented Jun 13, 2019 via email

@08irfan
Copy link

08irfan commented Jun 14, 2019

Thanks @suda for the reply.
issue resolved already :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment