致命错误:索引超出范围,当传递FirebaseData到tableviewcell



所以我正在工作的食物列表项目,我想通过mealname/mealpic/mealDescription到我的tableViewCell从FirebaseDatabase。

应该是这样的输入图像描述在这里

这是我的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)  
    }

它在运行时崩溃,在let menu = menus [indexPath.row]行抛出"Index out of range"错误

我知道这个索引超出范围的错误问题是一种常见问题但我自己做了一些研究,它们并没有真正的帮助

谁能指出哪里出了问题?由于

现在我成功地将firebaseddatabase中的内容传递给了tableview

但我有5个节点从Day1到Day5但我的tableview只显示一个(Day3)

在我的

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()

所以在print ("SNAP: (SNAP .key)")我得到从第1天到第5天的所有节点

但是在print (key)我只得到Day3

我认为是"if let menuDict"语句导致了这个问题

如果我去掉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()

运行时崩溃抛出"在展开可选值时意外发现nil "错误

我想我就要成功了,我会继续做更多的研究

任何提示都很感谢

尝试:-

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
    }

相关内容

  • 没有找到相关文章

最新更新