所以我正在做一个食物清单项目,我希望将来自FirebaseDatabase的mealname / mealpic / mealDescription传递给我的tableViewCell。
它将是这样的enter image description here
这是我的FirebaesDatabase结构
这是我在视图控制器中的代码
override func viewDidLoad() {
super.viewDidLoad()
WeekMenu.delegate = self
WeekMenu.dataSource = self
let url = GIDSignIn.sharedInstance().currentUser.profile.imageURL(withDimension: 100)
let data = try? Data(contentsOf: url!)
userpic.image = UIImage(data:data!)
DataService.ds.REF_MENUDATA.observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]
{
for snap in snapshot {
print("SNAP: \(snap)")
if let menuDict = snap.value as? Dictionary<String, AnyObject> {
let date = snap.key
let menu = menuContnet(weekmenudate: date, menuData: menuDict as! Dictionary<String, String>)
self.menus.append(menu)
}
}
}
})
self.WeekMenu.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let menu = menus [indexPath.row]
if let cell: WeekMenu = tableView.dequeueReusableCell(withIdentifier: "WeekMenu") as! WeekMenu {
cell.configureCell(menu: menu)
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "ToVari", sender: nil)
}
它在运行时崩溃并抛出一个&#34;指数超出范围&#34;第let menu = menus [indexPath.row]
行
我知道这个索引超出范围的错误问题是一种FAQ 但我自己做了一些研究,但他们并没有真正帮助
有谁可能指出可能出错的地方? 谢谢
更新
现在我已成功将FirebaseDatabase中的内容传递到我的tableview中。
但是我从第1天到第5天有5个节点,但我的tableview只显示了一个(第3天)
在我的
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menus.count
}
为什么会这样?
我的课程内容如下:
class menuContnet {
private var _menuKey: String!
private var _mealDes : String!
private var _mealPic : String!
private var _mealname : String!
private var _menuRef: FIRDatabaseReference!
var menuKey: String{
return _menuKey
}
var mealDes : String {
return _mealDes
}
var mealname : String {
return _mealname
}
var mealPic : String {
return _mealPic
}
init (mealDes: String, mealname: String, mealPic: String) {
self._mealDes = mealDes
self._mealname = mealname
self._mealPic = mealPic
}
init (menuKey: String, menuData : Dictionary <String, String>) {
self._menuKey = menuKey
if let mealname = menuData["mealname"] {
self._mealname = mealname
}
if let mealPic = menuData["mealPic"] {
self._mealPic = mealPic
}
if let mealDes = menuData["mealDes"] {
self._mealDes = mealDes
}
_menuRef = DataService.ds.REF_MENUS.child(_menuKey)
}
}
更新
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]
{
for snap in snapshot {
print ("SNAP: \(snap.key)")
if let menuDict = snap.value as? Dictionary<String, String> {
let key = snap.key
let menu = menuContnet(menuKey: key, menuData: menuDict)
print (key)
self.menus.append(menu)
self.WeekMenu.reloadData()
所以在打印时(&#34; SNAP:(snap.key)&#34;)我从第1天到第5天获得所有节点
但是在打印(键)我只得到第3天
我认为&#34;如果让menuDict&#34;声明导致问题
如果我摆脱&#34;如果&#34;并使我的代码看起来像这样
let menuDict = snap.value as? Dictionary<String, String>
let key = snap.key
let menu = menuContnet(menuKey: key, menuData: menuDict)
print (key)
self.menus.append(menu)
self.WeekMenu.reloadData()
它在运行时崩溃&#34;在解开一个Optional值时出乎意料地发现nil&#34;错误
我想我到了那里,我会继续做更多的研究
任何提示表示赞赏
答案 0 :(得分:0)
尝试: -
DataService.ds.REF_MENUDATA.observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]
{
for snap in snapshot {
print("SNAP: \(snap)")
if let menuDict = snap.value as? Dictionary<String, AnyObject> {
let date = snap.key
let menu = menuContnet(weekmenudate: date, menuData: menuDict as! Dictionary<String, String>)
self.menus.append(menu)
self.WeekMenu.reloadData() // This will reload your table to show your newly appended cell one at a time.
}
/*
if menus.count == snap.childrenCount {
self.WeekMenu.reloadData() //Will only reload your table after your entire requested database has been retrieved
}
*/
}
}
})
并改变你的: -
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menus.count ?? 0
}