[英]Get value property using Firebase in Swift
Using Firebase I am trying to get the "Product" value that belongs to the "Brand". 使用Firebase我正在尝试获取属于“品牌”的“产品”值。 For example if I click on cell where is "Brand"-"CATCH" I want to show new
tableView
with all the "CATCH" Products. 例如,如果我点击单元格“Brand” - “CATCH”,我想显示所有“CATCH”产品的新
tableView
。 How Can I achieve this? 我怎样才能做到这一点? This is the Firebase structure:
这是Firebase结构:
Here I am getting all the "Brands" names like this: 在这里,我得到了所有“品牌”这样的名字:
func parseSnusBrands(){
let ref = FIRDatabase.database().reference().child("Snuses").child("Brands")
ref.queryOrderedByKey().observeEventType(.Value, withBlock: { (snapshot) in
if snapshot.exists() {
if let all = (snapshot.value?.allKeys)! as? [String]{
self.snusBrandsArray = all
self.snusBrandsTableView.reloadData()
}
}
})
}
And like this I am trying to get the "Products" values using the allKeys
: 像这样我试图使用
allKeys
获取“Products”值:
func parseSnusProducts() {
let ref = FIRDatabase.database().reference().child("Snuses").child("Brands").child("Products")
ref.queryOrderedByKey().observeEventType(.Value, withBlock: { (snapshot) in
if snapshot.exists() {
if let all = (snapshot.value?.allValues)! as? [String]{
self.snusBrandsArray = all
self.snusBrandsTableView.reloadData()
}
}
})
}
This is how I detect the cell click: 这是我检测单元格点击的方式:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
parseSnusProducts()
}
I have 2 tableViews also and custom cell to show the "Products" 我还有2个tableViews和自定义单元格来显示“产品”
Is the cell detection right? 细胞检测对吗? How to achieve this?
怎么做到这一点? I don't see anything on the internet by Googling but maybe you know some links.
谷歌搜索网上没有看到任何东西,但也许你知道一些链接。
I think to get all values and keys in one array, you need to use [[String:AnyObject]]
我想在一个数组中获取所有值和键,你需要使用
[[String:AnyObject]]
var snusBrandsArray = [[String:AnyObject]]()
override func viewDidLoad() {
super.viewDidLoad()
let ref = FIRDatabase.database().reference().child("Snuses").child("Brands")
ref.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if snapshot.exists() {
if let all = (snapshot.value?.allKeys)! as? [String]{
for a in all{
if let products = snapshot.value![a] as? [[String:String]]{
self.snusBrandsArray.append(["key":a,"value":products])
}
}
self.yourtableview.reloadData()
}
}
})
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//cell declaration
print(snusBrandsArray[indexPath.row]["key"])
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("products at \(indexPath.row) --> \(snusBrandsArray[indexPath.row]["value"])")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.