fatal error: Index out of range, when passing FirebaseData to tablviewCell
fatal error: Index out of range, when passing FirebaseData to tablviewCell
所以我正在做一个食物清单项目,我想将 mealname/mealpic/mealDescription 从 FirebaseDatabase 传递到我的 tableViewCell。
它看起来像这样enter image description here
这是我的 FirebaesDatabase 结构
enter image description here
这是我在视图控制器中的代码
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" 错误
我知道这个索引超出范围错误问题是一种常见问题解答
但我自己做了一些研究,他们并没有真正帮助
谁能指出哪里出了问题?
谢谢
更新
现在我成功地将我在 FirebaseDatabase 中的内容传递到我的表视图中。
但是我从第 1 天到第 5 天有 5 个节点,但我的表视图只显示一个(第 3 天)
在我的
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menus.count
}
为什么会这样?
我的class的内容是这样的
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)") 我得到了从第 1 天到第 5 天的所有节点
但在打印(关键)时我只得到第 3 天
我认为 "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()
它在运行时崩溃并抛出 "unexpectedly found nil while unwrapping an Optional value" 错误
我想我快到了,我会继续做更多的研究
感谢任何提示
尝试:-
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
}
所以我正在做一个食物清单项目,我想将 mealname/mealpic/mealDescription 从 FirebaseDatabase 传递到我的 tableViewCell。
它看起来像这样enter image description here
这是我的 FirebaesDatabase 结构
enter image description here
这是我在视图控制器中的代码
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 中的内容传递到我的表视图中。
但是我从第 1 天到第 5 天有 5 个节点,但我的表视图只显示一个(第 3 天)
在我的
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menus.count
}
为什么会这样?
我的class的内容是这样的
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)") 我得到了从第 1 天到第 5 天的所有节点
但在打印(关键)时我只得到第 3 天
我认为 "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()
它在运行时崩溃并抛出 "unexpectedly found nil while unwrapping an Optional value" 错误
我想我快到了,我会继续做更多的研究
感谢任何提示
尝试:-
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
}