ios - 如何从 UITableView 中的数组中获取用户选择的值并将该值传递给服务器

标签 ios arrays swift uitableview dictionary

我有两个字典数组

    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/

相关文章:

iphone - 在 sqlite3 性能方面有什么更好的 : delete unneeded row or set it as not needed?

iphone - 使用 NSDocumentDirectory 存储图像不起作用?

c# - 检查数据库中的字符串以查看它是否包含数组中的项目

swift - 如何添加一条小直线(我的意思是像这样 : a̅ b̅ X̅) onto a character inside a string?

ios - 如何正确设置 uicollectionviewcells?

objective-c - 更改 UIImageView 的前面位置

ios - Cordova-推送通知 firebase 未出现在状态栏中,但在 android 中工作

c - 当数组传递给c中的函数时传递了什么?数组值或引用地址的副本?

c - 增加数组元素的值

ios - Int 不可转换为 T