【发布时间】:2019-05-21 16:12:23
【问题描述】:
我正在从 firebase 获取一些详细信息并在 UITableView 上显示它们。
每行/单元格应该显示 2 个标签,但是,它只显示一个。
我的主视图控制器:
import UIKit
import Firebase
class NewFoodCaloriesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var addFood: DatabaseReference!
@IBOutlet weak var txtNewFoodInsert: UITextField!
@IBOutlet weak var txtNewFoodCaloriesInsert: UITextField!
@IBOutlet weak var lblSuccess: UILabel!
@IBOutlet weak var tableFoodList: UITableView!
var foodList = [FoodModel]()
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foodList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
let food: FoodModel
food = foodList[indexPath.row]
cell.lblFoodType.text = food.foodname
cell.lblFoodCalories.text = food.foodcalories
//print("II")
return cell
}
我的 ViewControllerTableViewCell:
class ViewControllerTableViewCell: UITableViewCell {
@IBOutlet weak var lblFoodType: UILabel!
@IBOutlet weak var lblFoodCalories: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我的 FoodModel.swift
{
var foodid: String?
var foodname: String?
var foodcalories: String?
var userid: String?
init(foodid: String?, foodname: String?, foodcalories: String?, userid: String?)
{
self.foodid = foodid
self.foodname = foodname
self.foodcalories=foodcalories
self.userid = userid
}
}
需要在表格视图中显示 Foodname 和 FoodCalories 的预期结果,但现在它只在表格视图中显示 FoodName
【问题讨论】:
-
foodList数组中有多少个元素? -
可能有很多问题;我假设您的表格显示的是食物名称而不是食物卡路里?还是只显示一行?问题描述有点模糊。您还需要包含 Firebase 结构的 sn-p,以便我们知道您的数据是什么样的,并且包含您的 Firebase 代码非常重要,因为这可能是问题所在。要获取您的 Firebase 结构,请使用 Firebase 控制台->导出 JSON,然后复制并粘贴您的结构的 sn-p。
标签: ios swift firebase tableview fetch