2016-07-07 3 views
0

저는 firebase에서 .observeEventType을 통해 Swift에서 디스플레이 데이터를 업데이트하는 방법과 내가 가지고있는 사전 배열을 궁금합니다. 삭제 버튼 기능이있는 테이블보기가 있습니다. 그러나, 나는 사전의 배열이 아니라 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 가격 계층을 표시하려면 :

는 그리고 이것은있는 tableview에 표시하는 것입니다. 값을 제거해도 업데이트되지 않습니다.

+0

당신이에 데이터를 검색 중포 기지 호출을 추가 할 수 있습니다 tableview? – adolfosrs

+0

@adolfosrs 나는 문제가 cellForRowAtIndexPath에 있다고 믿는다. 그러나 나는 그것을하는 또 다른 방법을 생각할 수 없다 .. – askaale

+0

해결책을 여기서 볼 수 있니, @adolfosrs? – askaale

답변

0

코드에 약간의 디자인 문제가 있으며 사용자의 경우 TableView를 처리하는 방법에 대한 더 나은 방법을 찾기 시작할 것입니다. 그래서, 그것을 명심하십시오.

현재의 TableView 코드와

와 난 아무것도 그리워 didnt하는 경우, 당신은 당신의 데이터 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 이것은 내가 U를 도울 수있는 방법입니다. 다른 곳에서 데이터를 망칠 수 있습니다. – adolfosrs

관련 문제