Usage of UIPickerView in iOS-UI and solutions to linkage crashes

Let’s take a look at the UIPickerView style first:

The overall structure can be divided into how many columns of data there are, and how many rows of data each column has. If more than one column is displayed, the data structure is 1 array + the structure of N sub-arrays in this array.

Like Tableview, using UIPickerView requires complying with the control's proxy protocol:

<UIPickerViewDelegate>和<UIPickerViewDataSource>

The specific method code is as follows:

#pragma mark - pickerview delegate、datasource

// returns the number of 'columns' to display.返回有多少个columns
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return self.dataArray.count;
}

// returns the # of rows in each component.在component返回有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSArray *arr = self.dataArray[component];
    return arr.count;
}

// 返回pickerview component中row行的显示数据
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSArray *arr = self.dataArray[component];
    return arr[row];
}

// pickerview选中某行时的代理事件
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

If there is a need for linkage related to provinces and cities, for example, due to the calling order of the proxy method, there will be a problem of inconsistent linkage coordination between the two parties. You can add a private model object to store the data selected in the previous column that has not been updated. The specific code is as follows:

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) NSArray *dataArray;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerview;
@property (weak, nonatomic) IBOutlet UILabel *provinceLabel;
@property (weak, nonatomic) IBOutlet UILabel *citiesLable;

@property (nonatomic, strong) ProvinceModel *model;

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pickerview.delegate = self;
    self.pickerview.dataSource = self;
    
    [self pickerView:self.pickerview didSelectRow:0 inComponent:0];
}

#pragma mark - UIPickerview delegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        [self.pickerview selectRow:0 inComponent:1 animated:NO];
        [self.pickerview reloadComponent:1];
        NSInteger provinceIndex = [self.pickerview selectedRowInComponent:0];
        ProvinceModel *model = self.dataArray[provinceIndex];
        self.provinceLabel.text = model.province;
    }
    NSInteger cityIndex = [self.pickerview selectedRowInComponent:1];
    self.citiesLable.text = self.model.cities[cityIndex];
}

#pragma mark - UIPickerview datasource

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        ProvinceModel *model = self.dataArray[row];
        return model.province;
    }else{
        return self.model.cities[row];
    }
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.dataArray.count;
    }else{
        self.model = self.dataArray[[self.pickerview selectedRowInComponent:0]];
        return self.model.cities.count;
    }
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return  2;
}

#pragma mark - 懒加载数据数组

-(NSArray *)dataArray{
    if (_dataArray == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CitiData" ofType:@"plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *marr = [NSMutableArray arrayWithCapacity:array.count];
        for (NSDictionary *dictionary in array) {
            ProvinceModel *model = [ProvinceModel provinceModelWithDictionary:dictionary];
            [marr addObject:model];
        }
        _dataArray = marr;
    }
    return _dataArray;
}

@end

Guess you like

Origin blog.csdn.net/JustinZYP/article/details/124480931