繁体   English   中英

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

[英]How to add UIButton with UIPickerView cell?

我有一个自定义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{ }

更新数据代码:

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
}
- (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并向视图添加按钮。

OK,我知道,使用UIPickTableViewWrapperCell的Button click事件块

在此处输入图片说明

我的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM