【问题标题】:Tableview not showing my appended arrays, even if they are not emptyTableview 不显示我附加的数组,即使它们不为空
【发布时间】: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


    【解决方案1】:

    sectionMessages 包含 3 个数组的副本,使用这些数组在您分配 sectionMessages 值时所具有的任何值。

    如果初始化sectionMessages后需要修改这些数组,则需要直接在sectionMessages中修改,而不是修改原来的数组。

    或者你也可以放弃sectionMessages,改用类似的东西

    func messages(for section: Int) -> [String] {
        switch section {
        case 0: return helpMessage
        case 1: return threatMessage
        case 2: return accidentMessage
        default: return []
    }
    

    【讨论】:

    猜你喜欢
    • 2011-06-25
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多