gpt4 book ai didi

ios - 如何在单个选择器中管理国家/州/城市?

转载 作者:行者123 更新时间:2023-11-29 11:49:36 30 4
gpt4 key购买 nike

如何使用单一选择器 Objective-C 来管理国家/州/城市的值(value)?

当我尝试它时,它会因索引更改而崩溃。它适用于城市城市和州,但与城市有关。哪个库会更好用?

查看下面的代码

- (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"]in‌​tegerValue]; NSLog(@"province key :%d", provinceID);
}
}
}

最佳答案

我知道您要求提供 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])")
}

关于ios - 如何在单个选择器中管理国家/州/城市?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714133/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com