我正在使用swift开发iOS应用程序,在应用程序的firebase仪表板中有以下数据
Users =
{
"07a5fa11-2a09-455b-92bf-a86dcd9d3e3e" =
{
Name = "Hissah";
Category = "Art & Designe";
City = "Riyadh";
Email = "H@him.fm";
ShortDescription = "";
};
"08e5443c-cdde-4fda-8733-8c4fce75dd34" =
{
Name = "Sara";
Category = "Cheefs";
City = "Dubai";
Email = "Sara@gmail.com";
ShortDescription = "best cake ever . ";
};
如何将(城市)为“利雅得”的用户的(名称)检索到表视图?
提前谢谢。
最佳答案
将其作为一个简单的答案在环中抛出,并处理可用于填充tableView的数据源
let ref = Firebase(url:"https://your-app.firebaseio.com/users")
ref.queryOrderedByChild("City").queryEqualToValue("Riyadh")
.observeEventType(.Value, withBlock: { snapshot in
//iterate over all the values read in and add each name
// to an array
for child in snapshot.children {
let name = child.value["Name"] as! NSString
self.tableViewDataSourceArray.append(name)
}
//the tableView uses the tableViewDataSourceArray
// as it's dataSource
self.tableView.reloadData()
})
编辑:后续评论询问如何将文本添加到NSTextView
ref.queryOrderedByChild("City").queryEqualToValue("Riyadh")
.observeEventType(.Value, withBlock: { snapshot in
//iterate over all the values and add them to a string
var s = String()
for child in snapshot.children {
let name = child.value["Name"] as! NSString
s += name + "\n" // the \n puts each name on a line
}
//add the string we just build to a textView
let attrString = NSAttributedString(string: s)
self.myTextView.textStorage?.appendAttributedString(attrString)
})