我正在使用iOS7中的多选UIPickerView,并使用UITapGestureRecognizer进行多选。
在iOS6中,它的工作正常但在iOS7 toggleSelection:(UITapGestureRecognizer)方法中未被调用。
所以请给我解决这个问题的想法或建议。
请参阅我的以下代码。
- (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 *gestureRecog = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleSelection:)];
[gestureRecog setDelegate:self];
gestureRecog.numberOfTapsRequired = 1;
[gestureRecog.view setBackgroundColor:[UIColor redColor]];
// [singleTapGestureRecognizer.view setTag:row];
[cell addGestureRecognizer:gestureRecog];
}
if ([self.selectedItems indexOfObject:[NSNumber numberWithInt:row]] != NSNotFound) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.tag = row;
if (self.flag == 0 ) {
if (self.arraCountry.count > 0) {
cell.textLabel.text=[[self.arraCountry objectAtIndex:row] valueForKey:@"name"];
}
}
else if (self.flag == 1) {
if (self.arraStates.count > 0) {
cell.textLabel.text=[[self.arraStates objectAtIndex:row] valueForKey:@"name"];
}
}
else if (self.flag == 2) {
if (self.arrayCity.count > 0) {
cell.textLabel.text=[[self.arrayCity objectAtIndex:row] valueForKey:@"name"];
}
}
return cell;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)toggleSelection:(UITapGestureRecognizer *)recognizer {
NSNumber *row = [NSNumber numberWithInt:recognizer.view.tag];
NSUInteger index = [self.selectedItems indexOfObject:row];
if (index != NSNotFound) {
[self.selectedItems removeObjectAtIndex:index];
[(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryNone];
} else {
[self.selectedItems addObject:row];
[(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}