2017-06-21 46 views
0

我的问题是这样的:我可以从firebase加载所有东西,我可以将它附加到不同的数组。但我无法在我的桌面视图中显示它。当我使用section时,它不起作用,但是如果我将一个静态字符串插入到我的一个数组中,例如:accidentMessages:String = [“Hu”],那么Hu显然在tableview下的“accident”部分中 控制台它打印出我的消息从firebase的消息,所以字符串(消息)必须已被追加到我的数组。但由于某种原因,我无法在桌面视图中显示它。 以下是从firebase中获取并添加到阵列中的消息的控制台日志。 enter image description hereTableview不显示我附加的数组,即使它们不是空的

消息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! 
} 

回答

0

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 [] 
} 
+0

那么这里是我的git:https://github.com/Lukayne/Pushitt/blob/master/SkolprojektNr2/ViewController.swift – Lukayne

+0

谢谢!你让它工作,我只需要添加“sectionMessages = [0:helpMessage,1:threatMessage,2:accidentMessage]”到我的一个观察者方法中。 – Lukayne

相关问题