我有两个字典数组
1. Array of categories
[{
"Category_ID": "1",
"Category_en": "Event1",
},
{
"Category_ID": "4",
"Category_en": "Event4",
},
{
"Category_ID": "9",
"Category_en": "Event9",
}]
2.Array of cities
[{
"City_id": "1",
"City_name": "Place1",
},
{
"City_id": "2",
"City_name": "Place2",
},
{
"City_id": "4",
"City_name": "Place4",
}]
我在两个 tableView 中显示这些数组的值,用户可以选择多个项目,即他们想要的城市和类别。我如何获取用户选择的城市、类别并将该 id 值传递给 url 以从服务器获取结果
如果我通过此方法将选定的行作为数组获取,我如何将相应的 id
从城市和类别附加到 url
let list = tableView.indexPathsForSelectedRows() as? [NSIndexPath]
这是从服务器获取结果的url
example : category_id=1,9&city_id=2,4
http://www.events/events/searchEventsByCategory?category_id=1,9&city_id=2,4
谢谢你的帮助
最佳答案
假设您有一个 categoriesTableView、citiesTableView,并且您的数组名为 categoryArray 和 cityArray
//create string of selected categories
let list = categoriesTableView.indexPathsForSelectedRows() as? [NSIndexPath];
let categoryIds = "";
for (let indexPath in list) {
if (categoryIds.length == 0) {
categoryIds = categoryArray[indexPath.row]["Category_ID"])
continue
}
categoryIds = "\(categoryIds),\(categoryArray[indexPath.row]["Category_ID"])"
}
//create string of selected cities
list = citiesTableView.indexPathsForSelectedRows() as? [NSIndexPath];
let citiesIds = "";
for (let indexPath in list) {
if (citiesIds.length == 0) {
citiesIds = cityArray[indexPath.row]["City_id"])
continue
}
citiesIds = "\(citiesIds),\(cityArray[indexPath.row]["City_id"])"
}
你的网址将是这样的:
let url = "http://www.events/events/searchEventsByCategory?category_id=\(categoryIds)&city_id=\(citiesIds)"
关于ios - 如何从 UITableView 中的数组中获取用户选择的值并将该值传递给服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44676711/