UIPickerView ShowsSelectionIndicator 不起作用
Posted
技术标签:
【中文标题】UIPickerView ShowsSelectionIndicator 不起作用【英文标题】:UIPickerView ShowsSelectionIndicator does not work 【发布时间】:2017-08-31 12:33:42 【问题描述】:尽管据说指标从ios7开始总是显示,但我得到这样的东西:
这是我的实现:
class TimePicker: UIPickerView
override func awakeFromNib()
self.delegate = self
self.dataSource = self
self.showsSelectionIndicator = true
extension TimePicker: UIPickerViewDelegate
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
let minutes = [0,1,2,3,4,5,6,7,8,9,10]
let seconds = [0,5,10,15,20,25,30,35,40,45,50,55]
switch component
case 0: return minutes[row].description
default: return seconds[row].description
extension TimePicker: UIPickerViewDataSource
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 2
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
switch component
case 0:
return 11
default:
return 12
我的代码有什么问题,指标没有显示,这让它看起来很糟糕?
【问题讨论】:
【参考方案1】:您能否尝试使用 UIPickerViewDelegate 的其他替代委托方法来配置组件中的行视图,即
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
UILabel* label = nil;
if (view == nil)
view = [[UIView alloc] init];
view.backgroundColor = [UIColor blueColor];
label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 22)];
NSString *rowTextValue;
switch component
case 0: rowTextValue = [Nsstring StringWithFormat:@"%d",minutes[row]]; break;
default: rowTextValue = [Nsstring StringWithFormat:@"%d", seconds[row]]; break;
label.text = rowTextValue;
[view addSubview:label];
if (label == nil)
NSLog(@"%@",view.subviews);
label = view.subviews[0]; // I do get subviews here.
return view;
【讨论】:
因为我不是 swift 的人,你可以得到这个方法的 swift 版本作为 pickerView(_:viewForRow:forComponent:reusing:) 方法来提供组件行的内容。我发布这个是因为 Swift 和 ObjC 在理解上非常相似以上是关于UIPickerView ShowsSelectionIndicator 不起作用的主要内容,如果未能解决你的问题,请参考以下文章
如何从另一个 UIPickerview 更改 UIPickerView 的字符串?