Ios 如何从UITableView中的数组中获取用户选择的值并将该值传递给服务器
我有两组字典Ios 如何从UITableView中的数组中获取用户选择的值并将该值传递给服务器,ios,arrays,swift,uitableview,dictionary,Ios,Arrays,Swift,Uitableview,Dictionary,我有两组字典 1. Array of categories [{ "Category_ID": "1", "Category_en": "Event1", }, { "Category_ID": "4", "Category_en": "Event4", }, { "Category_ID": "9",
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",
}]
我在两个表视图中显示这些数组中的值,用户可以选择多个项目,表示他们想要的城市和类别。如何获取用户选择的城市、类别并将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
感谢您提供的任何帮助制作2个数组并将选定的索引目录添加到数组中 说出selectedcategories和selectedcities 在
在选择之后,通过添加for循环生成字符串生成一个数组,并将所选索引目录添加到数组中 说出selectedcategories和selectedcities 在
在选择之后,通过添加for循环来创建字符串假设您有一个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"])"
}
您的URL如下所示:
let url = "http://www.events/events/searchEventsByCategory?category_id=\(categoryIds)&city_id=\(citiesIds)"
假设您有一个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"])"
}
您的URL如下所示:
let url = "http://www.events/events/searchEventsByCategory?category_id=\(categoryIds)&city_id=\(citiesIds)"
您是否尝试过使用
tableView.indexPathsForSelectedRows
?谢谢。请查看我编辑的问题您是在询问如何构造url字符串吗?您是否尝试过使用tableView.indexPathsForSelectedRows
?谢谢。请看我编辑的问题。你是在问如何构造url字符串吗?谢谢。你能指出任何与此类似或相关的例子吗?谢谢。你能指出任何与此类似或相关的例子吗?告诉我什么不起作用?它创建了哪个URL?告诉我到底是什么不起作用?它创建了哪个URL?