如何使用UIPickerView单元格添加UIButton?

时间:2017-03-06 07:01:15

标签: ios uipickerview

我有一个自定义UIPickView,点击(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view自定义UIButton,但无法正常工作,我该怎么办?请帮助我们。感谢

我的代码:

  - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
        UIButton *button;
        if (!button) {
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 0, pickerView.bounds.size.width, 44.0);
            [button setTitle:_pickDataArray[row] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(pickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 1000 + row;
        }
        return button;
    }

    - (void)pickButtonAction:(UIButton *)button{
        NSLog(@"button.tag = %ld",(long)button.tag);
    }

不需要此代码

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ }

updata代码:

UIButton *button;
if (view == nil) {
    view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    view.userInteractionEnabled = YES;
    if (!button) {
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, pickerView.bounds.size.width, 44.0);
        [button setTitle:_pickDataArray[row] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(pickButtonAction:) forControlEvents:UIControlEventTouchDown];
        button.tag = 1000 + row;
        [view addSubview:button];
    }
}
return view;


- (void)pickButtonAction:(UIButton *)button{
    NSLog(@"button.tag = %ld",(long)button.tag);//always not executed
}

2 个答案:

答案 0 :(得分:2)

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
     if (view == nil) {
        view = [[UIView alloc] init]; 
        view.backgroundColor = [UIColor blueColor];
        if (!button) {
              button = [UIButton buttonWithType:UIButtonTypeCustom];
              button.frame = CGRectMake(0, 0, pickerView.bounds.size.width, 44.0);
              [button setTitle:_pickDataArray[row] forState:UIControlStateNormal];
              [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
              [button addTarget:self action:@selector(pickButtonAction:) forControlEvents:UIControlEventTouchDown];
              button.tag = 1000 + row;
             [view addSubview:button];
        }
     }

    return view;
}

您只是忘记返回UIView并在视图中添加按钮。

答案 1 :(得分:0)

好的,我明白了,用UIPickTableViewWrapperCell按钮点击事件块

enter image description here

我的代码:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    id view = [super hitTest:point withEvent:event];
    if ([view isKindOfClass:[UIView class]]) {
        return nil;
    }
    return [view hitTest:[self convertPoint:point toView:view] withEvent:event];
}