【问题标题】:How to receive touches on UIPickerview using viewForRow delegate?如何使用 viewForRow 委托接收对 UIPickerview 的触摸?
【发布时间】:2023-04-01 18:25:01
【问题描述】:

谁能帮帮我。我正在尝试在 UIPickerview(在 iOS 7 中)上实现多项选择。由于堆栈溢出的引用很少,我做了如下,

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

尝试后,它没有收到点击手势,因此toggleSelection方法没有被调用。

注意:UIpickerview 是作为 UITextField 的输入视图给出的

userSafeDoorTextfeild.inputView = [self doorSelectionPickerView];

【问题讨论】:

    标签: iphone ios7 uipickerview


    【解决方案1】:

    我解决了这个问题,可以帮助处于这种困境中的人。

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

    不是将点击手势添加到类范围选择器视图,而是在检查标记后将手势添加到 viewForRow 委托方法中返回的此方法选择器视图对象。

    【讨论】:

      猜你喜欢
      • 2017-02-01
      • 1970-01-01
      • 2018-01-24
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      相关资源
      最近更新 更多