我需要使用自定义结构的数组填充我的tableview,目前它只会使用相同的数组填充每个部分的表视图。
我追加的地方......
let shake = Item(name: "Shake", carbs: 20)
let fries = Item(name: "Fries", carbs: 30)
let pie = Item(name: "Pie", carbs: 23)
let beverages = Category(name: "Beverages", items: [shake])
let chips_fries = Category(name: "Chips & Fries", items: [fries])
let desserts = Category(name: "Desserts", items: [pie])
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])
restaurants = [a_w]
这是我尝试使用Items数组填充表视图...
var restaurants = [Restaurant]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
// Do any additional setup after loading the view.
}
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.section]
let item = category.items[indexPath.row]
cell.textLabel!.text = item.name
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return restaurants.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let restaurant = restaurants[section].categories[section]
return restaurant.items.count
}
}
只会用“摇晃”来填充每个类别。
答案 0 :(得分:0)
您必须使用indexPath.row
代替indexPath.section
。
但要更好地回答它,请更新您希望UI的外观。
你想要每个餐厅都是一个部分吗?如果是,您如何区分不同的类别?
如果您想要嵌套部分,请参阅本教程: http://sapandiwakar.in/nested-sections-in-uitableview/