不使用结构数组填充 tableview [英] Not populating tableview with structure array
本文介绍了不使用结构数组填充 tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要用结构数组填充我的 tableView.结构的第一个属性是名称.这是我试过的...
I need to populate my tableView with an array of a structure. The first property of the structure is the name. This is what I tried...
var menuArray:[Restaurant] = [Restaurant]()
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: [])
a_w = Restaurant(name: "A&W", categories: [beverages, chips_fries, desserts, other, sandwiches_burgers, sides])
let menuArray = [a_w]
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let currentCell = tableView.dequeueReusableCell(withIdentifier: "cell")
let currentRestaurant = menuArray[indexPath.row]
currentCell?.textLabel!.text = currentRestaurant.name
return currentCell!
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuArray.count
}
为什么它不会填充我的 tableView
Why won't it populate my tableView
这也是我的课...
import Foundation
struct Item {
let name: String
let carbs: Int
}
struct Category {
let name: String
let items: [Item]
}
struct Restaurant {
let name: String
let categories: [Category]
}
推荐答案
在这一行
let menuArray = [a_w]
您正在创建一个局部变量 menuArray
,它与代表数据源数组的同名属性不同.
you are creating a local variable menuArray
which is different from the property with the same name representing the data source array.
省略let
menuArray = [a_w]
PS:请使用比 a_w
更多的描述性变量名称.
PS: Please use more descriptive variable names than a_w
.
这篇关于不使用结构数组填充 tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文