2017-04-20 93 views
0

我正在爲我的一位家庭成員製作應用,以便他們能夠更好地管理他們的客戶,但遇到了一些問題。這是我第一次使用Firebase,我似乎無法讓我的代碼工作!我陷入困境的部分涉及Firebase的實時數據庫,而我正在使用Swift 3.1在XCode 8.3中工作。Firebase客戶端應用

代碼:

import UIKit 
import FirebaseCore 
import FirebaseDatabase 
import FirebaseAuth 

    var specClientId = "" 

    class MyCell: UITableViewCell { 

     @IBOutlet var nameCell: UILabel! 

     @IBOutlet var statusCell: UILabel! 

    } 

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

     @IBOutlet var tableView: UITableView! 

     var ref: FIRDatabaseReference! 

     var tableArray: [String] = [] 

     var clientId: [String] = [] 

     var statusArray:[String] = [] 

     @IBAction func signOut(_ sender: Any) { 

      UserDefaults.resetStandardUserDefaults() 

      performSegue(withIdentifier: "segueBackLogin", sender: self) 

     } 

     public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

      return tableArray.count 

     } 

     public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

      let cell = tableView.dequeueReusableCell(withIdentifier: "CellFront") as! MyCell 

      cell.nameCell.text = tableArray[indexPath.row] 

      cell.statusCell.text = statusArray[indexPath.row] 

      return cell 

     } 

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

      specClientId = clientId[indexPath.row] 

      ref.child("Users").child(specClientId).child("lastUpdate").removeValue() 

      performSegue(withIdentifier: "segue", sender: self) 

     } 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      if FIRApp.defaultApp() == nil { 
       FIRApp.configure() 
      } 

      ref = FIRDatabase.database().reference() 

      ref.child("Users").observeSingleEvent(of: .value, with: { (snapshot) in 

       let value = snapshot.value as? NSDictionary 

       let specificValues = value?.allKeys 

       self.tableArray.removeAll() 

       self.statusArray.removeAll() 

       self.clientId.removeAll() 

       var it = 0 

       for Uservalue in specificValues! { 

        self.tableArray.append("") 

        self.statusArray.append("") 

        self.clientId.append(Uservalue as! String) 

        self.ref.child("Users") 
         .child(Uservalue as! String) 
         .child("name") 
         .observeSingleEvent(of: .value, with: { (snapshot) in 

          let nameValue = snapshot.value as? String 

          self.tableArray.insert(nameValue!, at: it) 

          self.tableArray = self.tableArray.filter {$0 != ""} 

          self.tableView.reloadData() 

         }) { (error) in 

          print(error.localizedDescription) 

         } 

        self.ref.child("Users") 
         .child(Uservalue as! String) 
         .child("lastUpdate") 
         .observeSingleEvent(of: .value, with: { (snapshot) in 

          if let nameValue = snapshot.value as? String { 

           self.statusArray.insert("*", at: it) 

           self.tableView.reloadData() 

          } else { 

           self.statusArray.insert("", at: it) 

           self.tableView.reloadData() 

          } 

         }) { (error) in 

          print(error.localizedDescription) 

        } 

        it += 1 

       } 

      }) { (error) in 

       print(error.localizedDescription) 

      } 

     } 

     override func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
      // Dispose of any resources that can be recreated. 
     } 


    } 

我的主要問題是,當我得到了用戶的名字和他們LASTUPDATE狀態,數組列表不正確匹配和TableView中顯示錯誤信息,無論在哪個用戶有提交了更新。爲了解決這個問題,我嘗試在我的數組中使用insert方法,但現在應用程序崩潰了。以前,我使用的是append方法,但導致在TableView中顯示錯誤的信息。如果你們中的任何一位能夠幫助我解決這個問題,我將不勝感激。

注:應用程序崩潰由於StatusArray不具有元件作爲TableArray的相同的量。這是由TableArray具有一些沒有名稱的空元素引起的。

感謝, KPS

編輯1:

for Uservalue in specificValues! { 

       self.clientId.append(Uservalue as! String) 

       let user = User() 

       self.ref.child("Users") 
        .child(Uservalue as! String) 
        .observeSingleEvent(of: .value, with: { (snapshot) in 

         let nameValue = snapshot.value as? NSDictionary 

         let specNameValue = nameValue?.allKeys 

         var i = 0 

         while i < specNameValue!.count { 

          if specNameValue?[i] as? String == "name" { 

           user.name = nameValue?.allValues[i] as! String 

          } else if specNameValue?[i] as? String == "lastUpdate" { 

           user.status = "*" 

          } else if specNameValue?[i] as? String != "name" && specNameValue?[i] as? String != "lastUpdate" && specNameValue?[i] as? String != "message" && specNameValue?[i] as? String != "adminMessage" && specNameValue?[i] as? String != "photoURL" { 

           user.status = "" 

          } 

          i += 1 

         } 


        }) { (error) in 

         print(error.localizedDescription) 

        } 

       self.tableArray.append(user) 

       self.tableView.reloadData() 

      } 

回答

0

您的應用程序崩潰的主要原因是因爲在你的行單元格中要加載的第一個用戶後,重載以及電池預計statusArray已經有元素。

cell.nameCell.text = tableArray[indexPath.row] 
cell.statusCell.text = statusArray[indexPath.row] // fails here I assume 

這裏有幾個問題,我會盡力解決。

  • 您正在爲每個迭代的孩子立即重新加載表。這將是明智致電tableView.reloadData()
  • 附加元素,每個數組,然後一旦完成顯示的所有元素都是狀態獨立的名稱,你期待?如果數據是相關的,這將是明智創建一個簡單對象以容納這些數據,有數據的單一陣列的的tableView將用於它的DataSource
  • 一旦數據完全加載,你可以將相應數據進行排序然後重新加載數據源以解決從服務器中提取數據的問題。這就是爲什麼append(element:)是簡單而有用的原因

希望這有助於!這可能看起來有點多,但它肯定會對你自己的表現,組織和可讀性有益。

+0

嗨,我試圖重新加載數據後追加所有的元素,但數據永遠不會重新加載。另外,我認爲Object Idea非常好,但是我怎樣才能爲TableView做到這一點?我對iOS開發很新,如果這是Android,我已經完成了。謝謝 – kps2501

+0

哦,那麼我會創建一個對象數組,然後將一個包含數組和用戶狀態的對象附加到該數組?謝謝 – kps2501

+0

@ kps2501正確。沿着這些線的東西。如果數據是相關的,那麼是的,這會讓你的事情變得更簡單。這樣你只需要訪問數組中索引處的對象來填充單元格! – jnewkirk