我需要使用类别的名称填充我的tableview。当我填充tableview时,它显示了这个......
Category(name: "Sandwiches & Burgers", items: [])
我追加所有信息的地方......
let sortedKeys = newArray.sorted{$0.0 < $1.0}
let shake = Item(name: "Shake", carbs: 20)
let fries = Item(name: "Fries", carbs: 30)
let beverages = Category(name: "Beverages", items: [shake])
let chips_fries = Category(name: "Chips & Fries", items: [fries])
let desserts = Category(name: "Desserts", items: [])
let other = Category(name: "Other Menu Items", items: [])
let sandwiches_burgers = Category(name: "Sandwiches & Burgers", items: [])
let sides = Category(name: "Sides", items: [])
a_w = Restaurant(name: "A&W", categories: [beverages, chips_fries, desserts, other, sandwiches_burgers, sides])
menuArray = [a_w]
然后我的班级
struct Item {
let name: String
let carbs: Int
}
struct Category {
let name: String
let items: [Item]
}
struct Restaurant {
let name: String
let categories: [Category]
}
这是我填充的地方......
var currentRestaurant:Restaurant!
var menuArray:[Restaurant] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var array = currentRestaurant.categories
print(array)
let currentCell = tableView.dequeueReusableCell(withIdentifier: "cell")!
currentCell.textLabel?.text = String(describing: array[indexPath.item])
return currentCell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentRestaurant.categories.count
}
答案 0 :(得分:1)
查看您的数据源cellForRowAt
应该是:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let category = currentRestaurant.categories[indexPath.row]
cell.textLabel!.text = category.name
return cell
}
但是在重新加载表格视图之前的某个时刻,您必须将currentRestaurant
设置为menuArray[0]
实际上处理这种数据源的常用方法是在各个部分显示餐馆。
请将数据源数组声明为比menuArray
var restaurants = [Restaurant]()
然后返回numberOfSections
中的餐馆数量override func numberOfSections(in tableView: UITableView) -> Int {
return restaurants.count
}
以及numberOfRowsInSection
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let restaurant = restaurants[section]
return restaurant.categories.count
}
和cellForRow
是
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let restaurant = restaurants[indexPath.section]
let category = restaurant.categories[indexPath.row]
cell.textLabel!.text = category.name
return cell
}
通过这种方式,您根本不需要currentRestaurant
。
在viewDidLoad
填充数据源数组并重新加载表视图
override func viewDidLoad() {
super.viewDidLoad()
let shake = Item(name: "Shake", carbs: 20)
let fries = Item(name: "Fries", carbs: 30)
let beverages = Category(name: "Beverages", items: [shake])
let chips_fries = Category(name: "Chips & Fries", items: [fries])
let desserts = Category(name: "Desserts", items: [])
let other = Category(name: "Other Menu Items", items: [])
let sandwiches_burgers = Category(name: "Sandwiches & Burgers", items: [])
let sides = Category(name: "Sides", items: [])
restaurants = [Restaurant(name: "A&W", categories: [beverages, chips_fries, desserts, other, sandwiches_burgers, sides])]
tableView.reloadData()
}