【发布时间】:2017-01-28 12:37:38
【问题描述】:
我是 Swift 3 和 Firebase 的新手,因此在从两个不同的节点检索数据并将它们显示在一个表格视图单元格中时遇到了一些问题。我在 Stack Overflow 上发现了一些其他类似的问题,如下所示:
Firebase - iOS Swift: load table view cell with data retrieved from two separate child nodes
但无法将其应用到我的代码中,因为这些示例对于询问的人来说过于具体,或者我对 Firebase 缺乏了解,导致我无法继续提供所提供的答案。
我的应用程序是使用 Xcode-8 用 Swift 3 编写的,并使用 Firebase 进行数据持久化。该应用程序的目的是允许用户提交不同的锻炼计划以供其他用户使用。用户提交的程序有与他们相关联的作者的 uid,我打算使用它然后根据这个 uid 值从一个单独的节点检索用户的用户名。
我的 Firebase 设置:
"programs" : {
"-KYF3o3YD6F3FEXutuYH" : {
"content" : "Program Content Goes Here...",
"duration" : "4 Weeks",
"title" : "Chest Blast",
"type" : "Hypertrophy",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
},
"-KYF4ev88FQ2nEr6yTOW" : {
"content" : "Program Content Goes Here...",
"duration" : "6 Weeks",
"title" : "Full Back Workout",
"type" : "Strength",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
},
"-KZRYF9A8-8OHCNzOoPT" : {
"content" : "Eugene and Eamon",
"duration" : "4 Weeks",
"title" : "abc",
"type" : "abc",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
},
"-KbKNdrAcBarpaNoGf_e" : {
"content" : "Test",
"duration" : "test",
"title" : "New Test",
"type" : "test",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
},
"-KbKnXnyzj_EJp_wNw5y" : {
"content" : "1. Barbell Bench Press\n\nWhy it's on the list: You can generate the most power with barbell lifts, so the standard barbell bench allows you to move the most weight. It's also an easier lift to control than pressing with heavy dumbbells. The exercise is easy to spot and relatively easy to learn (if not master), There are plenty of bench-press programs you can follow to increase your strength.\n\n1. Barbell Bench Press\n\nWhy it's on the list: You can generate the most power with barbell lifts, so the standard barbell bench allows you to move the most weight. It's also an easier lift to control than pressing with heavy dumbbells. The exercise is easy to spot and relatively easy to learn (if not master), There are plenty of bench-press programs you can follow to increase your strength.",
"duration" : "1",
"title" : "1",
"type" : "1",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
}
},
"users" : {
"hds1WketiAUELVDOz1Dprvlu0KE3" : {
"username" : "Example"
},
"oLy9GOzDyKht7WWVZgpd3jPHxsE3" : {
"username" : "Test"
}
}
应用程序中的表格如下所示:
如您所见,此时作者仅由其 uid 指示。这很容易,因为我将它存储在与显示的所有其他数据相同的位置,但我需要使用该 uid 值来搜索另一个节点并获取与其关联的用户名并将其显示在现在显示 uid 的位置。
下面是我用来检索和显示数据的代码。我的主要问题是:
1:我可以使用什么查询来搜索用户节点以查找匹配的 uid,并仅获取该人的用户名并将其显示在表格视图中?
2:我应该将该查询放在代码中的什么位置?我想把它放在 func tableView() 方法中,因为每次将新帖子添加到单元格时我都可以搜索用户,这样我就不必制作另一个 NSMutable 数组来保存可能甚至没有帖子。非常感谢您提供的任何帮助。
@IBOutlet weak var programsTableView: UITableView!
var programs = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
self.programsTableView.delegate = self
self.programsTableView.dataSource = self
//Call load data method to populate table with data from Firebase
loadData()
}
//Method to load data from Firebase
func loadData(){
FIRDatabase.database().reference().child("programs").observeSingleEvent(of: .value, with: { (snapshot) in
//Snapshot holds value and it is casted to NS Dictionary
if let programsDictionary = snapshot.value as? [String: AnyObject]{
for program in programsDictionary{
self.programs.add(program.value)
}
self.programsTableView.reloadData()
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! AllProgramsTableViewCell
// Configure the cell...
let program = self.programs[indexPath.row] as! [String: AnyObject]
//Populate row
//Grab title and add it to cell
cell.titleLabel.text = program["title"] as? String
//Grab type and add it to cell
cell.typeLabel.text = program["type"] as? String
//Grab duration and add it to cell
cell.durationLabel.text = program["duration"] as? String
//Grab content and add it to cell
cell.contentTextView.text = program["content"] as? String
//Grab author and add it to cell
cell.authorLabel.text = program["uid"] as? String
return cell
}
【问题讨论】:
-
请删除您的 Firebase 结构的图片,并将实际的 Firebase 结构发布为文本,请不要使用图片。这可以从您的 Firebase 控制台->右侧的三个点->导出 JSON 中获得。使用文本很重要,因为它是可搜索的,并且有助于我们为您提供帮助,因为我们不必在答案中重新输入。
-
@Jay 我做了这些改变。道歉。
标签: ios swift firebase swift3 tableview