これは複雑に見えるかもしれませんが、実際はかなりシンプルなものでしょう。配列にコンテンツを追加できません
私は私のVCでtableviewCellにFirebaseの私の日付をパスしようとしています
は、ここに私のデータベース構造
"2016-10-24 MON" : {
"types" : {
"LUNCH" : {
"TypeA" : "2", //the amount
"TypeB" : "1",
"TypeC" : "1"
},
"DINNER" : {
"TypeA" : "1",
"TypeB" : "2",
"TypeC" : "3"
}
}
}
"2016-10-26 WED" : {
"types" : {
"LUNCH" : {
"TypeA" : "0",
"TypeB" : "2",
"TypeC" : "3"
},
"DINNER" : {
"TypeA" : "2",
"TypeB" : "2"
}
}
だ、これは私がしようとしています何私のコード
class orderDetail: UIViewController, UITableViewDelegate, UITableViewDataSource{
var theARRAY : [(DATES: String, TimeDeliv: String, AmountA:String, AmountB: String, AmountC: String)] = []
override func viewDidLoad() {
theREF.observeSingleEvent(of: .value, with: { (snapshot) in
//theREF is my Firebase Reference
if let Date = snapshot.value as? [String:AnyObject]{
for each in Date {
let orderdate = each.0 as! String
theREF.child(orderdate).observe(.value, with: { (snapshot) in
if snapshot.hasChild("LUNCH") {
theREF.child(orderdate).child("LUNCH").observe(.value, with: {(snapshot) in
if let snapDict = snapshot.value as? Dictionary <String, AnyObject> {
let Num1 = snapDict["TypeA"] as? String
let Num2 = snapDict["TypeB"] as? String
let Num3 = snapDict["typeC"] as? String
self.theARRAY.append((DATES: "\(orderdate)", TimeDeliv:"LUNCH", AmountA:"TypeA \(Num1)", AmountB:"TypeB \(Num2)", AmountC :"TypeC \(Num3)"))
}
})
print("LUNCH works \(snapshot)")
} else {
print ("NONE")
}
})
}
}
}
)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return theARRAY.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DetailCell") as! detailCell!
cell?.detailMealTypeA.text = theARRAY.AmountA
cell?.detailMealtypeB.text = theARRAY.AmountB
cell?.detailMealtypeC.text = theARRAY.AmountC
// these are from UItableViewCell file
return cell!
}
です私はデータベースの詳細をtheARRAY
に追加してからtableViewCellでそれらを靴にしたいと思っていますが、私は0
を続けていますnumberOfRowsInSection
cellForRowAt
にはnil
が機能します。
私はtheARRAY.append
を間違った行に呼び出していると思います。
誰かが私を助けてくれますか?
ところで私の厄介なコードを許してください。
「スナップショット」は印刷されていますか?はいの場合は、内容を追加した直後に配列のサイズを印刷してみてください。サイズが増加している場合は、追加が機能し、配列にはそれぞれのデータが含まれているので、単に 'tableview'をリロードする必要があります。 forループの直後に 'self.tableView.reloadData()'を追加してください。 – eshirima
@EmilDavid、これは私が必要なものです!どのような愚かな間違い:( おかげで、あなたは私のお尻を保存 – Ian