gpt4 book ai didi

swift - 将 FirebaseData 传递到 tablviewCell 时出现 fatal error : Index out of range,

转载 作者:行者123 更新时间:2023-11-30 12:58:40 24 4
gpt4 key购买 nike

所以我正在开发一个食物列表项目,我想将 mealname/mealpic/mealDescription 从 FirebaseDatabase 传递到我的 tableViewCell。

看起来像这样 enter image description here

这是我的Firebaes数据库结构

enter image description here

这是我在 View Controller 中的代码

    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] 行抛出“索引超出范围”错误

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

有人能指出可能出了什么问题吗?谢谢

更新

现在我成功地将 FirebaseDatabase 中的内容传递到我的表格 View 中。

但是我从 Day1 到 Day5 有 5 个节点,但我的表格 View 只显示一个(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()

所以在打印(“SNAP:(snap.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
}

关于swift - 将 FirebaseData 传递到 tablviewCell 时出现 fatal error : Index out of range,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40110240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com