gpt4 book ai didi

arrays - Firebase/Swift 在删除时更新 TableView 值?

转载 作者:行者123 更新时间:2023-11-30 13:15:47 24 4
gpt4 key购买 nike

我只是想知道如何通过 .observeEventType 以及我拥有的字典数组从 firebase 更新 Swift 中的显示数据。我有一个 TableView ,带有删除按钮功能。但是,我只能从 firebase 中删除值,而不是字典数组。我的viewDidLoad中有observeEventType,获取数据如下:

var dict = [String:String]()
dict["data"] = snapshot.value!["data"] as? String
array.append(dict)

但是,在 numberOfRowsInSection 中我有 array.count,并且收到计数错误的错误。我该如何解决这个问题?从字典数组中删除值,对整个数据进行遍历并显示新数据,或者我该怎么办?

顺便说一句:当应用程序崩溃时,我再次构建它 - 删除的值消失了,并且数据显示正确 - 但是,我只想删除部分顺利运行。

提前致谢。

完整代码:

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 个不同的价格等级之间进行选择。它显示在表格 View 中。

这将显示在表格 View 中:

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
}



}

这段代码看起来相当困惑,但我只想随时在顶部显示最高价格层。当我删除值时,它不会更新。

最佳答案

您的代码存在一些设计问题,我将开始寻找一种更好的方法来处理您的案例中的 TableView。所以,请记住这一点。

使用您当前的 TableView 代码,如果我没有错过任何内容,为了实现您正在寻找的内容,您应该如何检索数据:

func LoadValues() {
var curID = FIRAuth.auth()?.currentUser?.uid
ref.child("Users").child(curID!).child("Prices").observeEventType(.Value, withBlock: { snapshot 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.removeAll()
self.allTiers.append(self.dict2)
}
}

关于arrays - Firebase/Swift 在删除时更新 TableView 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38240934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com