swift - 无法从 Firestore 数据库加载分段控制表视图中的数据
问题描述
我正在尝试通过从 Firestore 数据库获取数据来填充分段控件的表视图,但由于某种原因我无法这样做,我正在尝试一个在线可用的示例,它具有预设数据,但在这里我正在检索数据来自 Firestore 数据库。
它也没有给出任何运行时错误,只是没有加载数据。下面是代码的视图控制器的屏幕截图。请协助
class FirstSegementViewController: UIViewController {
@IBOutlet var segmentControl:UISegmentedControl!
@IBOutlet var tableView: UITableView!
var s1Post:[s1] = []
var s2Post:[s2] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
retrieveAllPosts()
}
func retrieveAllPosts(){
let postsRef = Firestore.firestore().collection("posts").limit(to: 50)
postsRef.getDocuments { (snapshot, error) in
if let error = error {
print(error.localizedDescription)
} else {
if let snapshot = snapshot {
for document in snapshot.documents {
let data = document.data()
//self.postKey = document.documentID
let username = data["post_author_username"] as? String ?? ""
let postTitle = data["postTitle"] as? String ?? ""
let postcategory = data["postcategory"] as? String ?? ""
let postContent = data["postContent"] as? String ?? ""
let postAuthorProfilePicUrl = data["post_user_profile_pic_url"] as? String ?? ""
let postAuthorSpinnerC = data["post_author_spinnerC"] as? String
let newSourse = s1(_documentId: document.documentID, _username: username, _postTitle: postTitle, _postcategory: postcategory, _postContent: postContent, _postuserprofileImagUrl: postAuthorProfilePicUrl, _postAuthorSpinncerC: postAuthorSpinnerC)
self.s1Post.append(newSourse)
// print(self.postKey)
}
self.tableView.reloadData()
}
}
}
}
@IBAction func indexChanged(_ sender: UISegmentedControl) {
switch segmentControl.selectedSegmentIndex
{
case 0:
retrieveAllPosts()
// label1.text = "First Segment Selected"
case 1:
retrieveAllPosts()
// label1.text = "Second Segment Selected"
default:
break
}
//self.tableView.reloadData()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension FirstSegementViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var value = 0
switch segmentControl.selectedSegmentIndex{
case 0:
value = s1Post.count
break
case 1:
value = s2Post.count
break
default:
break
}
return value
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyWsPostCell
switch segmentControl.selectedSegmentIndex{
case 0:
cell.s11 = s1Post[indexPath.row]
break
case 1:
cell.s21 = s2Post[indexPath.row]
break
default:
break
}
return cell
}
}
解决方案
您似乎只为 self.s1Post 设置值。
推荐阅读
- java - 可在 JVM Scala 和 Scala.js 中使用的计时器
- sql-server - 报表服务器 2019 是否不可用
- windows - sc.exe 替代找到已停用的 Windows 服务?
- vb.net - 循环遍历类中的属性并分配值(VB.Net)
- typescript - 为什么在使用联合类型分隔不同类型的形状时出现错误?
- java - 在 Spring Security 有机会之前创建一个处理请求的 Vaadin 请求处理程序
- azure - Azure 数据库同步失败
- c++ - C++ 中 Google Pub/Sub 的消费者示例
- c++ - C++:为什么函数声明不能为其参数分配名称?
- java - 通过布尔条件中断while循环