2017-04-20 6 views
0

私は自分の家族の一員のためにアプリケーションを作っていたので、クライアントをよりうまく管理できるようになりましたが、いくつかの問題がありました。これは初めてFirebaseを使用していて、私のコードを動作させることができないようです!私が立ち往生している部分にはFirebaseのリアルタイムデータベースが関係しており、XCode 8.3とSwift 3.1で作業しています。Firebase Clients App

コード:

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. 
     } 


    } 

私の主な問題は、私は、ユーザーの名前と彼らの最終更新ステータスを取得する場合、配列のリストが正しく一致していないということで、テーブルビューは、ユーザーが持っているという点で間違った情報が表示されます彼らの更新を提出した。この問題を解決するために、配列内でinsertメソッドを使用しようとしましたが、現在アプリケーションがクラッシュします。以前はappendメソッドを使用していましたが、誤った情報がTableViewに表示されていました。あなたの誰かがこの問題で私を助けることができたら、私はそれを感謝します。

注:AppArrayは、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がそのデータソース用に使用する単一のデータ配列を持つことが賢明です
  • データが完全にロードされると、データソースをリロードして、サーバから順不同のデータを引き出す問題を解決します。これがappend(element:)がシンプルで便利な理由です。

うまくいけば、これが役に立ちます。それはもう少し作業のように見えるかもしれませんが、あなた自身のパフォーマンス、構成、可読性にとっては有益なことでしょう。

+0

こんにちは、私はすべての要素を追加した後にデータをリロードしようとしましたが、データはリロードされません。また、私はオブジェクトのアイデアは本当に良いと思うが、私はどのようにそのTableViewのために行くつもりですか?私はiOS開発にかなり新しいです。もしこれがアンドロイドなら、私はすでにそれをやっていたでしょう。ありがとう – kps2501

+0

ああ、私はオブジェクトの配列を作成し、配列とユーザーの状態の両方を含むオブジェクトでその配列に追加しますか?ありがとう – kps2501

+0

@ kps2501正解。それらの線に沿って何か。データが相互に関連している場合は、yesを指定すると少し簡単になります。そのようにすれば、配列に含まれるインデックスのオブジェクトにアクセスするだけで、セルに値を設定できます。 – jnewkirk

関連する問題