ios - UITableView reloadData() 不能快速工作?
问题描述
我有一个试图从 Firebase 填充的 tableView,但我遇到了 reloadData() 的问题,它不会刷新表格。
这是我的代码:
func showAnswers(pos: Int, name: String){
let n = name
answersReference.observe(DataEventType.value, with: {(snapshot) in
if(!snapshot.exists()){
}else{
let answers = snapshot.children
for answer in answers{
let answersFromDB = AnswerFromDBObject()
answersFromDB.setQuestion(question: (answer as! DataSnapshot).key)
let ansData = (answer as! DataSnapshot).children
for a in ansData{
answersFromDB.setAnswer(answer: (a as! DataSnapshot).key)
}
print("firebase answer: \(answersFromDB.getAnswer())")
self.answerFromDBObject += [answersFromDB]
}
print("array count: \(self.answerFromDBObject.count)")
self.userInfoAnwers.text = NSLocalizedString("users_questions", comment: "").replacingOccurrences(of: "(usernamehere)", with: n)
self.answersTable.reloadData()
}
})
}
还有我的 tableView 委托方法:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return answerFromDBObject.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "answersCell") as! SeeAnswersCell
let answer = answerFromDBObject[indexPath.row]
cell.question.text = answer.getQuestion()
cell.answer.text = answer.getAnswer()
return cell
}
这些是控制台上的打印方法:
firebase 答案:红色
firebase 答案:精细
firebase 答案:伦敦
阵列数:3
解决方案
在最后 } 之后试试这个
DispatchQueue.main.async { self.answersTable.reloadData() }
您这样做是为了能够在主线程中重新加载它
检查 tableView 委托和数据源已连接
如果您使用的是 Xib 单元,请检查它是否已注册
推荐阅读
- react-native - React Native:如何使用 `./gradlew build` 命令测试已弃用的功能
- python - 如何检查我是否已经回复了推文?
- r - 有任何理由使用 && 和 II 吗?
- python - python - 如何在python 3中向后声明代码并用某个字符替换一个字母?
- database - 我如何将非空字段添加到现有表中,其中现有表包含许多记录
- php - 我想通过 api 在 wordpress 上上传图片
- powershell - 适用于 Windows 性能记录器 (WPR) 的 Powershell 计时器
- parameters - 将 ddev 升级到 1.17.1 后,sequelpro 自动完成功能不起作用
- python - 未排序的文件名集
- java - com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链路故障 2021