私は、firebaseの表示データをfirebaseから.observeEventTypeに更新する方法と、自分が持っている辞書の配列をどうやって更新するのかと思っていました。私は削除ボタンの機能を持つテーブルビューを持っています。しかし、辞書の配列ではなく、firebaseから値を削除するだけです。私はこのように私のデータを取得し、私のviewDidLoadでobserveEventTypeを持っている:削除時のFirebase/Swift更新テーブルビューの値は?
var dict = [String:String]()
dict["data"] = snapshot.value!["data"] as? String
array.append(dict)
しかし、numberOfRowsInSectionに私はarray.countを持っている、と私はカウントが間違っているというエラーが発生します。どうすればこの問題を解決できますか?辞書の配列から値を削除すると、データ全体が偽装され、新しいデータが表示されるか、何をしますか?
Btw:アプリがクラッシュして再びビルドすると、削除された値がなくなり、データが正しく表示されますが、削除部分をスムーズにしたいだけです。
ありがとうございます。
トータルコード:
func LoadValues() {
var curID = FIRAuth.auth()?.currentUser?.uid
ref.child("Users").child(curID!).child("Prices").observeEventType(.Value) { (snapshot:FIRDataSnapshot) in
if let PermPrice = snapshot.value!["PermanentPrice"] as? Int {
self.dict2["PermanentPrice"] = String(PermPrice)
}
if let Price24 = snapshot.value!["24hours"] as? Int {
self.dict2["24hours"] = String(Price24)
}
if let Price12 = snapshot.value!["12hours"] as? Int {
self.dict2["12hours"] = String(Price12)
}
if let Price6 = snapshot.value!["6hours"] as? Int {
self.dict2["6hours"] = String(Price6)
}
if let Price2 = snapshot.value!["2hours"] as? Int {
self.dict2["2hours"] = String(Price2)
}
self.allTiers.append(self.dict2)
}
}
上記のコードは、ユーザが5つの異なる価格ティアの間で選択することができ、価格の層システムです。これはテーブルビューで表示されます。
そして、これはテーブルビューで表示することです:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell1 = tableView.dequeueReusableCellWithIdentifier("firstCell", forIndexPath: indexPath) as! TierCell1
cell1.deleteButton1.tag = 0
cell1.deleteButton1.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)
//let cell1 = tableView.dequeueReusableCellWithIdentifier("firstCell", forIndexPath: indexPath) as! TierCell1
//set the data here
if allTiers[0]["PermanentPrice"] != nil {
cell1.price1.text = "$" + String(allTiers[0]["PermanentPrice"]!)
cell1.text1.text = "Permanent"
PermTextTier = true
} else if allTiers[0]["24hours"] != nil {
cell1.price1.text = "$" + String(allTiers[0]["24hours"]!)
cell1.text1.text = "24 hours"
TextTier24 = true
} else if allTiers[0]["12hours"] != nil {
cell1.price1.text = "$" + String(allTiers[0]["12hours"]!)
cell1.text1.text = "12 hours"
TextTier12 = true
} else if allTiers[0]["6hours"] != nil {
cell1.price1.text = "$" + String(allTiers[0]["6hours"]!)
cell1.text1.text = "6 hours"
TextTier6 = true
} else if allTiers[0]["2hours"] != nil {
cell1.price1.text = "$" + String(allTiers[0]["2hours"]!)
cell1.text1.text = "2 hours"
TextTier2 = true
}
//cell1.text1.text = String(allTiers[0]["PermanentPrice"]!)
return cell1
}
if indexPath.row == 1 {
//let cell2: TierCell2 = TierCell2(style: UITableViewCellStyle.Default, reuseIdentifier: "secondCell") as! TierCell2
//set the data here
let cell2 = tableView.dequeueReusableCellWithIdentifier("secondCell", forIndexPath: indexPath) as! TierCell2
cell2.deleteButton2.tag = 1
cell2.deleteButton2.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)
if allTiers[0]["24hours"] != nil && TextTier24 == false {
cell2.price2.text = "$" + String(allTiers[0]["24hours"]!)
cell2.text2.text = "24 hours"
TextTier24 = true
} else if allTiers[0]["12hours"] != nil && TextTier12 == false {
cell2.price2.text = "$" + String(allTiers[0]["12hours"]!)
cell2.text2.text = "12 hours"
TextTier12 = true
} else if allTiers[0]["6hours"] != nil && TextTier6 == false {
cell2.price2.text = "$" + String(allTiers[0]["6hours"]!)
cell2.text2.text = "6 hours"
TextTier6 = true
} else if allTiers[0]["2hours"] != nil && TextTier2 == false {
cell2.price2.text = "$" + String(allTiers[0]["2hours"]!)
cell2.text2.text = "2 hours"
TextTier2 = true
}
return cell2
}
if indexPath.row == 2 {
//let cell2: TierCell2 = TierCell2(style: UITableViewCellStyle.Default, reuseIdentifier: "secondCell") as! TierCell2
//set the data here
let cell3 = tableView.dequeueReusableCellWithIdentifier("thirdCell", forIndexPath: indexPath) as! TierCell3
cell3.deleteButton3.tag = 2
cell3.deleteButton3.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)
if allTiers[0]["12hours"] != nil && TextTier12 == false {
cell3.price3.text = "$" + String(allTiers[0]["12hours"]!)
cell3.text3.text = "12 hours"
TextTier12 = true
} else if allTiers[0]["6hours"] != nil && TextTier6 == false {
cell3.price3.text = "$" + String(allTiers[0]["6hours"]!)
cell3.text3.text = "6 hours"
TextTier6 = true
} else if allTiers[0]["2hours"] != nil && TextTier2 == false {
cell3.price3.text = "$" + String(allTiers[0]["2hours"]!)
cell3.text3.text = "2 hours"
TextTier2 = true
}
return cell3
}
if indexPath.row == 3 {
//let cell2: TierCell2 = TierCell2(style: UITableViewCellStyle.Default, reuseIdentifier: "secondCell") as! TierCell2
//set the data here
let cell4 = tableView.dequeueReusableCellWithIdentifier("fourthCell", forIndexPath: indexPath) as! TierCell4
cell4.deleteButton4.tag = 3
cell4.deleteButton4.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)
if allTiers[0]["6hours"] != nil && TextTier6 == false {
cell4.price4.text = "$" + String(allTiers[0]["6hours"]!)
cell4.text4.text = "6 hours"
TextTier6 = true
} else if allTiers[0]["2hours"] != nil && TextTier2 == false {
cell4.price4.text = "$" + String(allTiers[0]["2hours"]!)
cell4.text4.text = "2 hours"
TextTier2 = true
}
return cell4
}
if indexPath.row == 4 {
//let cell2: TierCell2 = TierCell2(style: UITableViewCellStyle.Default, reuseIdentifier: "secondCell") as! TierCell2
//set the data here
let cell5 = tableView.dequeueReusableCellWithIdentifier("fifthCell", forIndexPath: indexPath) as! TierCell5
cell5.deleteButton5.tag = 4
cell5.deleteButton5.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)
if allTiers[0]["2hours"] != nil && TextTier2 == false {
cell5.price5.text = "$" + String(allTiers[0]["2hours"]!)
cell5.text5.text = "2 hours"
TextTier2 = true
}
return cell5
} else {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "none")
return cell
}
}
このコードはかなり乱雑に見えますが、私はいつでも、上にhigest価格ティアを表示したいです。値を削除すると、これは更新されません。
あなたがデータを取得firebase呼び出しを追加することができますテーブルビュー? – adolfosrs
@adolfosrs私は問題がcellForRowAtIndexPathにあると信じていますが、私はそれを行う別の方法を考えることはできません.. – askaale
ここで解決策が見えますか? – askaale