检索名称从火力地堡,以表视图
问题描述:
这里的仪表盘为iOS应用程序:检索名称从火力地堡,以表视图
{
"users" : {
"0b71693a-4f16-4759-acd6-706da1a466a0" : {
"Picker" : "Jubail",
"details" : "iOS second",
"name" : "Huda"
},
"16b13b1e-0025-4eee-a590-5bbacc52071c" : {
"Picker" : "Jeddah",
"details" : "Hellom from the Internet",
"name" : "Rania"
},
"394b6555-1838-4565-87ac-e423c3b89cf1" : {
"Picker" : "Jubail",
"details" : "",
"name" : "Marwa"
},
}
}
我想检索其选择器是朱拜勒到表格视图单元所有用户的名字!
我是如此初学者,我不知道该怎么做! 我想,这是我做的代码:
import UIKit
class CookingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var BusinessName:[String: String] = [String:String]()
let ref = Firebase (url: "https://mariahfinaltest.firebaseio.com")
@IBOutlet weak var CookingTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
ref.queryOrderedByChild("Picker").queryEqualToValue("Riyadh")
.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
self.BusinessName = child.value["name"] as! [String: String]
}
self.CookingTableView.reloadData()
})
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5 //It should not be 5 here! How Can I make it as long as cells?
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.CookingTableView.dequeueReusableCellWithIdentifier("CookingCell", forIndexPath: indexPath) as! CookingTableViewCell
var keys: Array = Array(self.BusinessName.keys)
cell.BusinessNameLabel.text = BusinessName[keys[indexPath.row]] as String!
return cell
}
}
我不断收到错误! 我不知道什么是错的!
答
你的代码是非常接近
它是最有可能的崩溃是由于
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5 //It should not be 5 here! How Can I make it as long as cells?
}
这是告诉你的tableView它有5个项目,如果它不其不会导致超出范围的值和崩溃。
所以这里是简化的设计模式。
var namesArray: [String]
ref.queryOrderedByChild("Picker").queryEqualToValue("Riyadh")
.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
let name = child.value["name"] as! String
self.namesArray.append(name)
}
self.CookingTableView.reloadData()
})
那么你的tableView委托方法
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return namesArray.count //return the number of items in the array
}
和
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mycell", forIndexPath: indexPath) as! UITableViewCell
let name = namesArray[indexPath.row]
cell.textLabel?.text = name
return cell
}
我明白我最后的错误,它的工作,也有一个与火力URL的一个问题!我没有在网址中添加“/ users”。 – Mariah
@Jay请回答这个问题http://*.com/questions/42646255/retrieve-all-child-value-to-a-label-in-table-view-cell –
@Xcodian你有问题吗?如果是这样,请发布它,以便我们可以看看您的使用案例! – Jay