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