2016-12-05 180 views
-1

我的應用程序中有一個tableView,它返回我的Firebase數據庫中用戶節點內的所有用戶。下面是我的數據庫樹的圖片,以及相關的代碼。我如何才能讓tableView只返回numberId設置爲1的用戶。因爲我的數據庫中只有2個用戶,所以我希望它返回一個用戶,而不是numberId設置爲2的用戶。此刻它會返回所有的用戶。我將不勝感激任何幫助。謝謝。如何過濾數據並僅返回相關用戶?

enter image description here

class User: NSObject { 

    var name: String? 
    var email: String? 
    var numberId: String? 
    var password: String? 
    var profileImageUrl: String? 
} 

class UsersController: UITableViewController { 

    let cellId = "cellId" 
    var users = [User]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel)) 

     tableView.register(UserCell.self, forCellReuseIdentifier: "cellId") 

     tableView.tableFooterView = UIView() 

     fetchUser() 
    } 

    func fetchUser() { 
     FIRDatabase.database().reference().child("users").observe(.childAdded, with: { (snapshot) in 

      if let dictionary = snapshot.value as? [String: AnyObject] { 
       let user = User() 

       user.setValuesForKeys(dictionary) 

       self.users.append(user) 

       DispatchQueue.main.async(execute: { 
        self.tableView.reloadData() 
       }) 
      } 

     }, withCancel: nil) 
    } 

    func handleCancel() { 
     dismiss(animated: true, completion: nil) 

    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return users.count 
    } 

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

     let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell 

     let user = users[indexPath.row] 
     cell.textLabel?.text = user.name 
     cell .detailTextLabel?.text = user.email 

     if let profileImageUrl = user.profileImageUrl { 

      cell.profileImageView.loadImageUsingCachWithUrlString(urlString: profileImageUrl) 
     } 

     return cell 
    } 

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return 56 
    } 
} 

class UserCell: UITableViewCell { 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     textLabel?.frame = CGRect(x: 58, y: (textLabel?.frame.origin.y)! - 2, width: (textLabel?.frame.width)!, height: (textLabel?.frame.height)!) 

     detailTextLabel?.frame = CGRect(x: 58, y: (detailTextLabel?.frame.origin.y)! + 2, width: (detailTextLabel?.frame.width)!, height: (detailTextLabel?.frame.height)!) 
    } 

    let profileImageView: UIImageView = { 
     let image = UIImageView() 
     image.contentMode = .scaleAspectFill 
     image.layer.masksToBounds = true 
     image.layer.cornerRadius = 20 
     image.translatesAutoresizingMaskIntoConstraints = false 
     return image 
    }() 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) 

     setupProfileImageView() 
    } 

    func setupProfileImageView() { 
     addSubview(profileImageView) 

     profileImageView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true 
     profileImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true 
     profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true 
     profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

回答

1

您需要使用queryOrderedByChildqueryEqualToValue

func fetchUser() { 
    FIRDatabase.database().reference().child("users").queryOrderedByChild("numberId").queryEqualToValue(1).observe(.childAdded, with: { (snapshot) in 

     if let dictionary = snapshot.value as? [String: AnyObject] { 
      let user = User() 

      user.setValuesForKeys(dictionary) 

      self.users.append(user) 

      DispatchQueue.main.async(execute: { 
       self.tableView.reloadData() 
      }) 
     } 

    }, withCancel: nil) 
} 
+1

謝謝。在Swift 3中,該行將是FIRDatabase.database()。reference()。child(「users」)。queryOrdered(byChild:「numberId」)。q​​ueryEqual(toValue:1).observe(.childAdded,with:{ )在 –

0

你可以有想法來自here,我不知道SWIFT如何編寫代碼,但..你可以有想法,希望您可以編寫代碼一樣聰明。

+0

這個問題被標記爲Swift –

+0

當你不熟悉語言時,請不要添加答案,這有助於保持SO清潔並使人們更容易找到正確的答案。 –

相關問題