【发布时间】:2017-06-21 00:06:41
【问题描述】:
我的问题是:我可以从 firebase 加载所有内容,我可以将其附加到不同的数组中。但我无法让它显示在我的表格视图中。当我使用部分时它不起作用,但是如果我将一个静态字符串插入到我的数组之一中,例如:accidentMessages: String = ["Hu"] ,那么 Hu 在“意外”部分下的表格视图中可见 在控制台中,它从 firebase 打印出我的消息,因此必须将字符串(消息)附加到我的数组中。但由于某种原因,我无法在 tableview 中展示它。 这是从 firebase 获取并添加到数组中的消息的控制台日志。enter image description here
消息 1,应该在“帮助”中,消息 2 应该在威胁中,消息 3 应该在意外中。但它不显示,它只显示在控制台中
var helpMessage: [String] = []
var threatMessage: [String] = []
var accidentMessage: [String] = ["Hu"]
var sectionMessages: [Int: [String]] = [:]
let sectionTitle = ["Help:", "Threat:", "Accident"]
sectionMessages = [0: helpMessage, 1: threatMessage, 2: accidentMessage]
func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitle.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (sectionMessages[section]?.count)!
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = UIColor.orange
let image = UIImageView(image: sectionImages[section])
image.frame = CGRect(x: 5, y: 5, width: 35, height: 35)
view.addSubview(image)
let label = UILabel()
label.text = sectionTitle[section]
label.frame = CGRect(x: 45, y: 5, width: 100, height: 35)
view.addSubview(label)
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 45
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
}
cell!.textLabel?.text = sectionMessages[indexPath.section]![indexPath.row]
return cell!
}
【问题讨论】:
标签: swift uitableview firebase firebase-realtime-database tableview