2016-07-07 29 views
0

我只是想知道如何通过.observeEventType - 和我拥有的字典数组来更新Swift中的显示数据。我有一个表格视图,带有删除按钮功能。但是,我只能从firebase中删除值,而不是字典数组。我有observeEventType在我viewDidLoad中,让我的数据是这样的:Firebase/Swift更新表视图的删除值?

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个不同的价格层之间进行选择。显示在表格视图中。

而且这是在实现代码如下显示:

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价格层,在任何时间。当我删除一个值时,这不会更新。

+0

你能添加检索数据所涉及的火力召唤的tableview? – adolfosrs

+0

@adolfosrs我相信问题在于cellForRowAtIndexPath,但我想不出另一种方式去做它.. – askaale

+0

你碰巧在这里看到一个解决方案,@adolfosrs? – askaale

回答

0

你的代码有一些设计问题,我会开始寻找更好的方法来处理你的案例中的TableView。所以,记住它。

根据您当前的TableView中的代码,如果我没有错过任何东西,达到你在找什么这是你应该如何retrieveing数据:

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) 
    } 
} 
+0

这似乎并没有解决我的问题。删除行后,表视图会更新为错误的值。 – askaale

+0

@askaale这几乎是我可以帮助你。你可能会在别的地方搞乱你的数据。 – adolfosrs