2017-06-21 5 views
0

私の問題はこれではない:私は別の配列に付加することができますfirebaseからすべてを読み込むことができます。しかし、私はそれを私のテーブルビューに表示することはできません。セクションを使用すると動作しませんが、私の配列の1つに静的な文字列を挿入すると、accidentMessages:String = ["Hu"]のように表示され、テーブルビューの "accident"セクションに表示されます。 それは、文字列(メッセージ)のでfirebaseからの私のメッセージを出力しますコンソール私の配列に追加されている必要があります。しかし何らかの理由で私はテーブルビューでそれを紹介することはできません。 ここ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! 
} 

答えて

0

sectionMessagesは、これらの配列は、あなたがsectionMessagesの値を割り当てられた時に持っていた値がどのような使用して、3つの配列のコピーが含まれています。

sectionMessagesの初期化後にこれらのアレイを変更する必要がある場合は、元のアレイを変更するのではなく、sectionMessagesに直接変更する必要があります。

それともsectionMessagesを捨て、代わりに私のgitのださてここで

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

のようなものを使用することができますhttps://github.com/Lukayne/Pushitt/blob/master/SkolprojektNr2/ViewController.swift – Lukayne

+0

ありがとうございました!あなたは私のオブザーバーメソッドの1つに "sectionMessages = [0:helpMessage、1:threatMessage、2:accidentMessage]"を追加しなければなりませんでした。 – Lukayne

関連する問題