无法仅使用名称填充 tableview [英] cannot populate tableview with just the name
问题描述
我需要只用类别的名称来填充我的 tableview.当我填充 tableview 时,它会显示这个...
I need to populate my tableview with just the name of the Categories. When I populate the tableview it shows this...
Category(name: "Sandwiches & Burgers", items: [])
在这里我附加了我的所有信息......
Heres where I append all of my information...
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]
}
这里是我填充的地方...
Here's where I populate...
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
}
推荐答案
查看你的数据源 cellForRowAt
应该是:
Looking at your data source cellForRowAt
is supposed to be:
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
}
但是在重新加载 table view 之前的某个时候,您必须将 currentRestaurant
设置为 menuArray[0]
But at some point before reloading the table view you have to set currentRestaurant
to menuArray[0]
实际上处理这种数据源的通常方法是将餐厅分块显示.
Actually the usual way to handle this kind of data source is to displays the restaurants in sections.
请将数据源数组声明为比menuArray
Please declare the data source array to some more descriptive name than menuArray
var restaurants = [Restaurant]()
然后返回numberOfSections中的餐厅数量
Then return the number of restaurants in 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
.
With this way you don't need currentRestaurant
at all.
在viewDidLoad
中填充数据源数组并重新加载表格视图
In viewDidLoad
populate the data source array and reload the table view
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()
}
这篇关于无法仅使用名称填充 tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!