Ios 根据swift中的类别在标题视图中显示数据
服务器响应:Ios 根据swift中的类别在标题视图中显示数据,ios,swift,Ios,Swift,服务器响应: { "success": { "allDishes": [ { "_id": "5dd78586c2f2871d84061929", "dishType": "non_veg", "recommended": true, "isCustomizable": false, "is
{
"success": {
"allDishes": [
{
"_id": "5dd78586c2f2871d84061929",
"dishType": "non_veg",
"recommended": true,
"isCustomizable": false,
"isAddons": false,
"availability": true,
"restaurantId": "5dad401b52c8ff200caf41da",
"categoryId": "5dc553ffd7b692257cebd170",
"categoryName": "north indian",
"subcategoryId": "5dc556ebcf881d19f4369f77",
"subcategoryName": "subtest",
"name": "Hand Fry",
"price": 100,
"dishImage": "/images/dish/1574405510680-1574405429836.jpg",
"customizable": [],
"addons": []
},
{
"_id": "5df9da5bc20d011e7c394f36",
"dishType": "veg",
"recommended": true,
"isCustomizable": true,
"isAddons": true,
"availability": true,
"restaurantId": "5dad401b52c8ff200caf41da",
"categoryId": "5dc553ffd7b692257cebd170",
"categoryName": "north indian",
"subcategoryId": "5dc556ebcf881d19f4369f77",
"subcategoryName": "subtest",
"name": "Rajdeep Special",
"price": 130,
"dishImage": "/images/dish/1576655451745-1576655324988.jpg",
"customizable": [
{
"_id": "5df9da5bc20d011e7c394f38",
"price": 20,
"title": "250 GM"
},
{
"_id": "5df9da5bc20d011e7c394f37",
"price": 35,
"title": "500 GM"
}
],
"addons": [
{
"_id": "5df9da5bc20d011e7c394f3a",
"price": 10,
"title": "Ice"
},
{
"_id": "5df9da5bc20d011e7c394f39",
"price": 30,
"title": "Tea"
}
]
},
{
"_id": "5df725448b8247182080ec47",
"dishType": "non_veg",
"recommended": false,
"isCustomizable": false,
"isAddons": false,
"availability": false,
"restaurantId": "5dad401b52c8ff200caf41da",
"categoryId": "5dc68d68ab42841948a56dd5",
"categoryName": "chinese",
"subcategoryId": "",
"subcategoryName": "",
"name": "This Is Or",
"price": 20,
"dishImage": "/images/dish/1576478019834-1576477996438.jpg",
"customizable": [],
"addons": []
},
{
"_id": "5df7233dbac9d01b3022f846",
"dishType": "veg",
"recommended": true,
"isCustomizable": false,
"isAddons": false,
"availability": true,
"restaurantId": "5dad401b52c8ff200caf41da",
"categoryId": "5dc8fa090535a7112c7b8cd4",
"categoryName": "sweets",
"subcategoryId": "5dcbd3268a276d0accc4cc75",
"subcategoryName": "laddu",
"name": "laddu n",
"price": 30,
"dishImage": "/images/dish/1576477501434-1576477479394.jpg",
"customizable": [],
"addons": []
},
{
"_id": "5df777d6ac1fc41844eaa2e3",
"dishType": "veg",
"recommended": false,
"isCustomizable": false,
"isAddons": false,
"availability": true,
"restaurantId": "5dad401b52c8ff200caf41da",
"categoryId": "5dc8fa090535a7112c7b8cd4",
"categoryName": "sweets",
"subcategoryId": "5dcbd3268a276d0accc4cc75",
"subcategoryName": "laddu",
"name": "laddu Wala ",
"price": 30,
"dishImage": "/images/dish/1576499158059-1576499116976.jpg",
"customizable": [],
"addons": []
},
{
"_id": "5df721df466f951b044d588f",
"dishType": "veg",
"recommended": true,
"isCustomizable": false,
"isAddons": false,
"availability": false,
"restaurantId": "5dad401b52c8ff200caf41da",
"categoryId": "5dc8fa090535a7112c7b8cd4",
"categoryName": "sweets",
"subcategoryId": "5dce9db6a1b36c03e019c22f",
"subcategoryName": "milk cake",
"name": "Milk ",
"price": 30,
"dishImage": "/images/dish/1576477151887-1576477133177.jpg",
"customizable": [],
"addons": []
}
],
"restaurentData": {
"_id": "5dad401b52c8ff200caf41da",
"name": "Trump Restaurant",
"firmName": "Tramp",
"email": "eggwalas@gmail.com",
"phoneNo": "8282828282",
"address": "Jaipur",
"profile_pic": "/images/restaurant/profile/1572937985224-1572937977064.jpg",
"lat": 26.852755861231476,
"long": 75.80524798482656,
"fssai": "1234567891"
}
}
}
代码我做了:
extension ResturantController : UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return categoryNameArr.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return categoryNameArr[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allDishArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let data = self.allDishArray[indexPath.row] as! NSDictionary
let cell = tableView.dequeueReusableCell(withIdentifier: "DishListCell", for: indexPath) as! DishListCell
for i in 0..<categoryNameArr.count {
let catName = categoryNameArr[i]
print("categoryNameToFind :: ",catName)
for j in 0..<allDishArray.count {
let dishes = self.allDishArray[j] as! NSDictionary
if let catNameFound = dishes.value(forKey: "categoryName") as? String{
//print("catNameFound :: ",catNameFound)
if catNameFound == catName {
print("Category Found")
}
}
}
}
if let dishType = data.value(forKey: "dishType") as? String{
if dishType == "veg"{
cell.imgVegNonVeg.image = UIImage(named: "veg")
}else if dishType == "non_veg"{
cell.imgVegNonVeg.image = UIImage(named: "non-veg")
}else{
cell.imgVegNonVeg.isHidden = true
}
}else{
cell.imgVegNonVeg.isHidden = true
}
if let dishName = data.value(forKey: "name") as? String{
cell.lblDishName.text = dishName
}else{
cell.lblDishName.text = "NO DATA"
}
if let price = data.value(forKey: "price") as? Int{
cell.lblDishPrice.text = "₹ " + String (price)
}else{
cell.lblDishPrice.text = "NO DATA"
}
return cell
}
}
扩展ResturantController:UITableViewDataSource、UITableViewDelegate{
func numberOfSections(在tableView:UITableView中)->Int{
返回categoryNameArr.count
}
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
返回类别名称[节]
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回allDishArray.count
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
将data=self.allDishArray[indexPath.row]设为!NSDictionary
让cell=tableView.dequeueReusableCell(标识符为:“DishListCell”,for:indexath)作为!DishListCell
对于0中的i..首先使用Codable
解析JSON。您可以使用以下模型解析数据
struct Root: Decodable {
let allDishes: [Dish]
let restaurentData: Restaurant
enum CodingKeys: String, CodingKey {
case success, allDishes, restaurentData
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let success = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .success)
allDishes = try success.decode([Dish].self, forKey: .allDishes)
restaurentData = try success.decode(Restaurant.self, forKey: .restaurentData)
}
}
struct Dish: Decodable {
let categoryName: String
let price: Double
let name: String
}
struct Restaurant: Decodable {
let name: String
}
在上面的模型中,我只添加了一些属性,您可以添加任何想要从JSON解析的属性
现在,在ViewController
中创建一个变量alldishesar
,该变量将用作表视图的数据源
var allDishesArr = [(String, [Dish])]()
接下来,使用上述创建的模型解析JSON,并将响应保存在AllDishesar
中。使用字典的init(grouping:by:)
根据categoryName
对菜肴进行分组,然后使用映射(\u:)
修改响应,就像这样
do {
let response = try JSONDecoder().decode(Root.self, from: data)
self.allDishesArr = Dictionary(grouping: response.allDishes) { $0.categoryName }.map({ ($0.key, $0.value) })
} catch {
print(error)
}
最后,您的UITableViewDataSource
方法如下:
func numberOfSections(in tableView: UITableView) -> Int {
return allDishesArr.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allDishesArr[section].1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DishListCell", for: indexPath) as! DishListCell
let dish = allDishesArr[indexPath.section].1[indexPath.row]
cell.lblDishName.text = dish.name
//add rest of the code
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let categoryName = allDishesArr[section].0
return categoryName
}
显示您尝试使用的代码对数组进行分组。这非常简单。使用代码更新查询您可能希望使用自定义UITableView标头并使用分组的TableView。这是否回答了您的问题?