如何从 Web api 获取特定数据到 table 视图部分?
How to get specific datas from web api to the table view sections?
我正在使用网络 api 获取食物名称和食物类别。我如何才能将我的早餐类别部分仅与早餐、汉堡、甜点、饮料、沙拉相匹配。问题是每个部分一次又一次地包含所有食物。
食品类别
struct FoodCategory: Decodable {
var id: Int
var Title: String //Category Title
}
两个型号都有食品类别标题。
食物
struct Food: Codable {
let id, SubCategoryId: Int
let CategoryTitle: String //Category Title
let Price, OldPrice: Decimal
let ProductTitle: String //Food Name
let Stock: Int
let Details: String
let DetailsList: [String]
}
FoodCategoryServiceResponse
"ResultList": [
{
"id": 1,
"Title": "Kahvaltılar"
},
{
"id": 5,
"Title": "Hamburgerler"
},
{
"id": 6,
"Title": "İçecekler"
},
{
"id": 7,
"Title": "Tatlılar"
},
{
"id": 9,
"Title": "Salatalar"
}
]
FoodServiceResponse
"ResultList": [
{
"id": 114,
"SubCategoryId": 28,
"SubCategoryTitle": "Hafta İçi Kahvaltısı",
"CategoryTitle": "Kahvaltılar",
"Title": null,
"Price": 18,
"OldPrice": 0,
"PriceString": " 18,00 ₺",
"OldPriceString": " 0,00 ₺",
"ProductTitle": "Hafta İçi Kahvaltısı",
"IsIndexView": false,
"Description": "Ekmek Üzeri Çırpılmış Yumurta, 1 Dilim Beyaz Peynir, Çilek Reçeli, 1 Bardak Çay",
"Stock": 0,
} ]
表格视图代码
var foodData = [Food]()
var foodCategoryData = [FoodCategory]()
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return foodCategoryData[section].Title
}
func numberOfSections(in tableView: UITableView) -> Int {
return foodCategoryData.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foodData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood") as! MainFoodTitleTableViewCell
let foodList = foodData[indexPath.row]
// let food = foods.filter({[=16=].category == sections[indexPath.section]})[indexPath.row]
cell.titleLabel.text = foodList.ProductTitle
cell.priceLabel.text = foodList.PriceString
return cell
}
创建一个代表您的部分的数组
let sections = ["Breakfast", "Hamburger", "Drinks", "Dessert", "Salad"]
然后将函数更改为
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionName = sections[section]
return foods.filter({ [=10=].category == sectionName }).count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood") as! MainFoodTitleTableViewCell
food = foods.filter({ [=10=].name == sections[indexPath.section] })[indexPath.row]
cell.title = food.name
cell.price = food.price
return cell
}
我推荐使用两个结构作为数据模型
struct Category {
let name : String
let foods : [Food]
}
struct Food {
let name : String
let price : Double
}
let categories = [
Category(name: "Breakfast", foods : [
Food(name: "Hafta İçi Kahvaltısı", price: 18.0),
Food(name: "Pazar Kahvaltısı", price: 25.0),
Food(name: "Diyet Kahvaltı", price: 22.0),
Food(name: "Köy Kahvaltısı", price: 15.0),
Food(name: "Ege Kahvaltısı", price: 30.0)]),
Category(name: "Hamburger", foods : [
Food(name: "Big TezBurger", price: 26.0),
Food(name: "TezRoyal", price: 24.0),
Food(name: "Double TezzBurger", price: 17.0),
Food(name: "TezChicken", price: 21.0),
Food(name: "Köfteburger", price: 28.0)])
]
这大大减少了 table 视图数据源和委托方法中的代码
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section].name
}
func numberOfSections(in tableView: UITableView) -> Int {
return categories.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories[section].foods.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood", for: indexPath) as! MainFoodTitleTableViewCell
let category = categories[indexPath.section]
let food = category.foods[indexPath.row]
cell.title = food.name
cell.price = food.price
return cell
}
您在 cellForRowAt
中的代码无论如何都没有意义。每个部分的每一行都会调用一次此方法。
我正在使用网络 api 获取食物名称和食物类别。我如何才能将我的早餐类别部分仅与早餐、汉堡、甜点、饮料、沙拉相匹配。问题是每个部分一次又一次地包含所有食物。
食品类别
struct FoodCategory: Decodable {
var id: Int
var Title: String //Category Title
}
两个型号都有食品类别标题。
食物
struct Food: Codable {
let id, SubCategoryId: Int
let CategoryTitle: String //Category Title
let Price, OldPrice: Decimal
let ProductTitle: String //Food Name
let Stock: Int
let Details: String
let DetailsList: [String]
}
FoodCategoryServiceResponse
"ResultList": [
{
"id": 1,
"Title": "Kahvaltılar"
},
{
"id": 5,
"Title": "Hamburgerler"
},
{
"id": 6,
"Title": "İçecekler"
},
{
"id": 7,
"Title": "Tatlılar"
},
{
"id": 9,
"Title": "Salatalar"
}
]
FoodServiceResponse
"ResultList": [
{
"id": 114,
"SubCategoryId": 28,
"SubCategoryTitle": "Hafta İçi Kahvaltısı",
"CategoryTitle": "Kahvaltılar",
"Title": null,
"Price": 18,
"OldPrice": 0,
"PriceString": " 18,00 ₺",
"OldPriceString": " 0,00 ₺",
"ProductTitle": "Hafta İçi Kahvaltısı",
"IsIndexView": false,
"Description": "Ekmek Üzeri Çırpılmış Yumurta, 1 Dilim Beyaz Peynir, Çilek Reçeli, 1 Bardak Çay",
"Stock": 0,
} ]
表格视图代码
var foodData = [Food]()
var foodCategoryData = [FoodCategory]()
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return foodCategoryData[section].Title
}
func numberOfSections(in tableView: UITableView) -> Int {
return foodCategoryData.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foodData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood") as! MainFoodTitleTableViewCell
let foodList = foodData[indexPath.row]
// let food = foods.filter({[=16=].category == sections[indexPath.section]})[indexPath.row]
cell.titleLabel.text = foodList.ProductTitle
cell.priceLabel.text = foodList.PriceString
return cell
}
创建一个代表您的部分的数组
let sections = ["Breakfast", "Hamburger", "Drinks", "Dessert", "Salad"]
然后将函数更改为
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionName = sections[section]
return foods.filter({ [=10=].category == sectionName }).count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood") as! MainFoodTitleTableViewCell
food = foods.filter({ [=10=].name == sections[indexPath.section] })[indexPath.row]
cell.title = food.name
cell.price = food.price
return cell
}
我推荐使用两个结构作为数据模型
struct Category {
let name : String
let foods : [Food]
}
struct Food {
let name : String
let price : Double
}
let categories = [
Category(name: "Breakfast", foods : [
Food(name: "Hafta İçi Kahvaltısı", price: 18.0),
Food(name: "Pazar Kahvaltısı", price: 25.0),
Food(name: "Diyet Kahvaltı", price: 22.0),
Food(name: "Köy Kahvaltısı", price: 15.0),
Food(name: "Ege Kahvaltısı", price: 30.0)]),
Category(name: "Hamburger", foods : [
Food(name: "Big TezBurger", price: 26.0),
Food(name: "TezRoyal", price: 24.0),
Food(name: "Double TezzBurger", price: 17.0),
Food(name: "TezChicken", price: 21.0),
Food(name: "Köfteburger", price: 28.0)])
]
这大大减少了 table 视图数据源和委托方法中的代码
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section].name
}
func numberOfSections(in tableView: UITableView) -> Int {
return categories.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories[section].foods.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood", for: indexPath) as! MainFoodTitleTableViewCell
let category = categories[indexPath.section]
let food = category.foods[indexPath.row]
cell.title = food.name
cell.price = food.price
return cell
}
您在 cellForRowAt
中的代码无论如何都没有意义。每个部分的每一行都会调用一次此方法。