我在做城市选择这个项目时,当我快速的滚动省份名称和城市名称的时候会出现越界错误,有没有那种比较好的方法去分别控制这俩列的滚动状态。
解决方案 »
- 弱弱的问下为啥iphone上不支持java开发
- iphone图片移动
- 如何使两个按钮不能同时选中
- IOS app 被杀掉的时候调用的是什么方法?
- ISO URLSession 锁屏后报错 Operation not permitted
- collection view怎么在四周留有空间?
- xib编译不通过???!!!!
- 关于NSBundle.pathForResource()取本地文件路径问题
- 如何代码实现点击按钮弹出pickerview,并把选择的值穿到fieldtext中
- Organization Name和Organization Identify怎么写?
- 问一个关于下载文件的问题
- iOS 真机测试问题:Ineligible Devices
先参考这个博文看看:http://ikrboy.iteye.com/blog/2003127
NSInteger index = pickerView.tag;
NSInteger rows = 0;
if (index == 1001) {
rows = _genderPickerViewArray.count;
}else if (index == 1002){
if (component == 0) {
rows = _cityPickerViewArray.count;
}else{
NSInteger selectedRow = [pickerView selectedRowInComponent:0];
rows = ((ProvinceModel*)_cityPickerViewArray[selectedRow]).citys.count;
}
}
return rows;
}-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
NSInteger index = pickerView.tag;
NSInteger groups = 0;
if (index == 1001) {
groups = 1;
}else if (index == 1002){
groups = 2;
}
return groups;
}#pragma - UIPickerView Delegate-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSInteger index = pickerView.tag;
NSString * title = @"";
if (index == 1001) {
title = _genderPickerViewArray[row];
}else if (index == 1002){
if (component == 0) {
ProvinceModel * model = (ProvinceModel*)_cityPickerViewArray[row];
title = model.provinceName;
}else{
NSInteger selectedRow = [pickerView selectedRowInComponent:0];
ProvinceModel * provModel = (ProvinceModel*)_cityPickerViewArray[selectedRow];
// 数组越界保护
if (row >= provModel.citys.count) {
title = @"";
}else{
CityModel * cityModel = (CityModel*)provModel.citys[row];
title = cityModel.cityName;
}
}
}
return title;
}- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSInteger index = pickerView.tag;
if (index == 1001) {
_genderLabel.text = _genderPickerViewArray[row];
_genderNumber = [self getGenderNumWithString:_genderLabel.text];
}else if (index == 1002){
if (component == 0) {
// component=0 每次变化, component=1刷新一次, 且默认选中index=0
// 同时调用一次本方法, 解决界面刷新问题.
[_pickerViewCity reloadComponent:1];
[_pickerViewCity selectRow:0 inComponent:1 animated:NO];
[self pickerView:pickerView didSelectRow:[pickerView selectedRowInComponent:1] inComponent:1];
}else{
NSInteger selectedRow = [pickerView selectedRowInComponent:0];
ProvinceModel * provModel = (ProvinceModel*)_cityPickerViewArray[selectedRow];
// 数组越界保护
if (row >= provModel.citys.count) {
_cityLabel.text = @"";
}else{
CityModel * cityModel = (CityModel*)provModel.citys[row];
_cityLabel.text = cityModel.cityName;
}
}
}
}
配合
[NSObject cancelPreviousPerformRequestsWithTarget:self];
来实现
我提供一个思路:使用一个NSArray保存上一次的城市数据,然后对此nsarray操作。