如何使用单一选择器Objective-C管理国家/州/州的价值?
当我尝试它时,它会在索引更改时崩溃。在城市和国家工作,但是和市有关系。哪个库可以更好地工作?
请参阅下面的代码
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
self.txtOfficeLocation.text = self.provinceArray[row];
for (int i = 0; i < self.provinceKeyValueDic.count; i++) {
if ([self.provinceKeyValueDic.allKeys[i] isEqualToString:self.provinceArray[row]]){
provinceID = [[self.provinceKeyValueDic.allValues[i]valueForKey:@"id"]integerValue]; NSLog(@"province key :%d", provinceID);
}
}
}
答案 0 :(得分:1)
我知道您已经要求使用Objective-C代码,但这里有一个快速的例子 - 关键元素很容易翻译。
我不知道你是如何设置数据的,但我创建了一些像这样的
struct StateData
{
var state : String
var city : [String] = []
}
struct CountryData
{
var country : String
var state : [StateData] = []
}
var pickerData : [CountryData] = []
您可以按照自己喜欢的方式填充
var stateData = CountryData(country: "UK", state: [StateData(state: "Midlothian", city: ["Edinburgh", "Bathgate", "Livingston"])])
stateData.state.append(StateData(state: "Strathclyde", city: ["Glasgow", "Prestwick", "Lanark"]))
pickerData.append(stateData)
stateData = CountryData(country: "USA", state: [StateData(state: "Alabama", city: ["Montgomery", "Birmingham"])])
stateData.state.append(StateData(state: "Alaska", city: ["Juneau", "Anchorage"]))
stateData.state.append(StateData(state: "Arizona", city: ["Phoenix", "Another city", "City 3"]))
pickerData.append(stateData)
我认为关键是要跟踪你选择的内容
var countrySelected : Int = 0
var stateSelected : Int = 0
var citySelected : Int = 0
pickerView方法需要看起来像这样
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 3 // country, state, city
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
switch component
{
case 0: // country
return pickerData.count
case 1: // state
return pickerData[countrySelected].state.count
case 2: // city
return pickerData[countrySelected].state[stateSelected].city.count
default:
return 0
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
switch component
{
case 0: // country
return pickerData[row].country
case 1: // state
return pickerData[countrySelected].state[row].state
case 2: // city
return pickerData[countrySelected].state[stateSelected].city[row]
default:
return ""
}
}
有趣的是确保当您更改国家/地区或更改状态时,您不会最终得到指向新显示中不存在的值的州或城市索引 - 如同你选择了一个州的第三个城市,然后搬到只有两个城市的州。这将产生您已经看到的越界错误。你可以像这样修复它
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
switch component {
case 0: // country
countrySelected = pickerView.selectedRow(inComponent: 0)
stateSelected = 0
citySelected = 0
pickerView.reloadComponent(1)
pickerView.selectRow(0, inComponent: 1, animated: true)
pickerView.reloadComponent(2)
pickerView.selectRow(0, inComponent: 2, animated: true)
case 1: // country
stateSelected = pickerView.selectedRow(inComponent: 1)
citySelected = 0
pickerView.reloadComponent(2)
pickerView.selectRow(0, inComponent: 2, animated: true)
case 2: // country
citySelected = pickerView.selectedRow(inComponent: 2)
default:
break
}
print("\(pickerData[countrySelected].country), \(pickerData[countrySelected].state[stateSelected].state), \(pickerData[countrySelected].state[stateSelected].city[citySelected])")
}