Ios 如何使用Swift3使用从Firestore集合检索的数据填充TableView
我对使用swift(以及一般的编码)进行IOS开发非常陌生,我想知道如何使用从Firestore文档检索到的数据填充我的tableview单元格 我正在开发的应用程序是一个用于实践的个人项目,它是一个基本的新闻应用程序,可以从firestore集合中加载文档。我试图从中加载信息的集合称为“新闻” 首先,以下是我迄今为止所取得的成就:Ios 如何使用Swift3使用从Firestore集合检索的数据填充TableView,ios,swift,uitableview,firebase,google-cloud-firestore,Ios,Swift,Uitableview,Firebase,Google Cloud Firestore,我对使用swift(以及一般的编码)进行IOS开发非常陌生,我想知道如何使用从Firestore文档检索到的数据填充我的tableview单元格 我正在开发的应用程序是一个用于实践的个人项目,它是一个基本的新闻应用程序,可以从firestore集合中加载文档。我试图从中加载信息的集合称为“新闻” 首先,以下是我迄今为止所取得的成就: //So first I have a struct struct Jobs { var title:String var des
//So first I have a struct
struct Jobs {
var title:String
var description:String
}
class JobsTableViewController: UITableViewController {
var db:Firestore!
var jobs: Jobs?
var numOfCells = 0
override func viewDidLoad() {
super.viewDidLoad()
db = Firestore.firestore()
loadData()
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
func loadData(){
db.collection("news").getDocuments { (querySnapshot, error) in
if let error = error
{
print("\(error.localizedDescription)")
}
else
{
for document in (querySnapshot?.documents)!
{
if let Title = document.data()["title"] as? String
{
print(Title) //I have this here to see if the new was being retrieved and it was.
self.title = Title
self.numOfCells += 1
}
}
DispatchQueue.main.async
{
self.tableView.reloadData()
}
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numOfCells
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = title
return cell
}
“news”集合中有3个文档,当我点击它并加载它时,这三个文档应该显示在Jobs选项卡中(我在Jobs选项卡中显示了新闻,因为我第一次想看看我是否能够检索数据,另外我还担心弄乱我在news上已有的代码)
当我切换到“作业”选项卡(即包含数据的tableview所在的位置)时
如果您注意到我的选项卡栏的名称也从作业更改为检索文档的标题
所以我的问题是:
1) 如何将所有文档检索到tableview中的单个单元格中
2) 如何使选项卡栏不会将其文本(如上所示)更改为文档的标题
3) 我的代码到底出了什么问题?为什么它错了?它做了什么
4) 我可以对我的代码进行哪些改进,为什么需要这些改进?(就像我说的,我只是在学习这一切)
奖金问题:
5) 单击单元格时,如何使标题和说明都显示在弹出窗口中
谢谢。在全球范围内创造一系列就业机会
var jobsArray = [Jobs]()
从加载数据中的数据库填充数组
for document in (querySnapshot?.documents)! {
if let Title = document.data()["title"] as? String {
print(Title)
//self.title = Title
let job = Jobs()
job.title = Title
jobsArray.append(job)
//self.numOfCells += 1
}
}
使用数组填充tableview
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return jobsArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let job = jobsArray[inedexPath.row]
cell.textLabel?.text = job.title
return cell
}
对于这一点:我将如何同时拥有标题和描述
单击单元格时是否显示在弹出窗口中
实现UITableView的委派功能
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//retreive job data from array
let job = jobsArray[inedexPath.row]
//show an alert with title and description
showAlert(title: String, desc: String )
}
func showAlert(title: String, desc: String) {
let alert = UIAlertController(title: title, message: mess, preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
这将显示iOS的默认警报框
在此处查看有关显示警报/弹出窗口的更多信息
或者如果你想要一些新奇的东西
一个非常灵活的消息栏,用于显示支持swift 4的弹出窗口/警报。hey@Jonathan James.M如果这有助于您,请将其标记为已批准的答案,谢谢。哎哟,我的糟糕,使用它还是新手