我正在尝试在 UIPickerView 中重新使用带有子视图的视图:
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
{
UILabel* label = nil;
if (view == nil) {
view = [[UIView alloc] init];
label = [[UILabel alloc] init];
[view addSubview:label];
}
if (label == nil) {
label = view.subviews[0]; // Exception here because there are no subviews
}
...
如果我的“reusingView”UIView 在入口处设置,我希望它保留(字面上和比喻上!)我添加的子视图,UILabel。但是,在从头开始设置前几个屏幕视图之后,我被一个(回收的,我假设)非零“reusingView”调用,但它没有任何子视图,所以我崩溃了当试图获取其现有标签来更改它时。
我是不是误会了什么?