UIPickerView数据联动

来源:互联网 发布:java获取http请求body 编辑:程序博客网 时间:2024/05/29 18:30
1,越界错误的解决方法
    (1) 在数据模型中写一个 过滤方法
     - (NSString *)cityNameWithIndex: (NSInteger)index
     {
          
// 判断索引是否越界          if (index < self.cities.count) {               return self.cities[index];          }else{               return nil;          }


     }
     (2) 刷新列表
     //选中component&row

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {    // 只有第0列变化需要刷新第1列的内容    if (component == 0) {        [pickerView reloadComponent:1];    }        // 设置文本标签的内容,显示当前选择的城市!    // 获取当前选中的城市名称    // 1> 第0列选中的省    int pRow = [pickerView selectedRowInComponent:0];    // 2> 第1列选中的市    int cRow = [pickerView selectedRowInComponent:1];        NSString *cityName = [self.provinces[pRow] cityNameWithIndex:cRow];        self.cityLabel.text = cityName;}


0 0
原创粉丝点击