我添加了一个选择器视图,dataSource和委托如下:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return _datas.count;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return _datas[component].count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat width = SCREEN_W / _datas.count;
return width;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return kActionPickerRowHeight;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, SCREEN_W / _datas.count, kActionPickerRowHeight);
label.textAlignment = NSTextAlignmentCenter;
label.text = [NSString stringWithFormat:@"%@",_datas[component][row]];
label.font = [UIFont systemFontOfSize:12.f];
label.layer.borderWidth = 1.f;
return label;
}
但是选择器显示如下:
右侧有一个约150px的白色边框。
我会显示行的边框和选择器的边框以进行调试
答案 0 :(得分:1)
更新方法
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat width = pickerView.frame.width / _datas.count;
return width;
}
答案 1 :(得分:0)
我发现导致问题的原因是:我没有将选择器视图的帧设置为及时更正值。当我在 - (void)layoutSubviews中编写框架设置代码时,它在调用dataSource的方法之后被调用,因此它无法正确显示。因此,我加上[self layoutIfNeed]在初始化后手动调用layoutSubviews,问题解决了。