Search code examples
iphoneios7uipickerview

How to receive touches on UIPickerview using viewForRow delegate?


Could anyone help me out. I'm trying to implement multiple selection on UIPickerview(in iOS 7). With few references from the stack overflow ,i have done as below,

- (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;       
        cell.userInteractionEnabled = YES;

        UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleSelection:)];
        singleTapGestureRecognizer.numberOfTapsRequired = 1;
        singleTapGestureRecognizer.delegate= self;
        [cell addGestureRecognizer:singleTapGestureRecognizer];
        [self.doorSelectionPickerView addGestureRecognizer:singleTapGestureRecognizer];
    }

    //TO DO FOR CHECKMARK    

    cell.textLabel.text = @"1";

    return cell;
}

After trying this, its not receiving the tap gesture, and thus toggleSelection method is not invoked.

Note: UIpickerview is given as input view of UITextField

userSafeDoorTextfeild.inputView = [self doorSelectionPickerView];

Solution

  • I fixed the issue and could help someone who are in this troubled situation.

    -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:
     (NSInteger)row  forComponent:(NSInteger)component 
     reusingView:(UIView *)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;
        cell.userInteractionEnabled = YES;
    
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]
                                              initWithTarget:self action:@selector(showTimePicker:)];
        [recognizer setNumberOfTapsRequired:1];
        [recognizer setDelegate:self];
    
    
        if(pickerView.tag == DOORSELECTION_PICKER_TAG){
            NSLog(@"its door selection category");
            *[pickerView addGestureRecognizer:recognizer];*
        }
    }
    
     }
    

    Instead of adding the tap gesture to the class scope picker view, add the gesture to this method picker view object returned in viewForRow delegate method after checking the tag.