[英]How do I load Static UITableView Headers into my app from Firebase Firestore?
I have a firebase firestore database set up to store quiz data for my iOS app. 我已设置一个Firebase Firestore数据库来存储我的iOS应用程序的测验数据。 My quiz data is supposed to load into a grouped static table view, with the section header being the question and each cell in the section being an answer choice.
我的测验数据应该加载到分组的静态表视图中,其中节标题是问题,而节中的每个单元格是答案选择。 My problem is I can not figure out how to load data into the header.
我的问题是我不知道如何将数据加载到标头中。
https://imgur.com/a/SpZhtUL https://imgur.com/a/SpZhtUL
This is what my quiz table view looks like right now. 这就是我的测验表视图现在的样子。 The data loaded in is actually supposed to be where "Header" is.
实际上,加载的数据应该在“ Header”所在的位置。
var quizArray = [Quiz]()
var db: Firestore!
let cellId = "cellId"
let headerId = "headerId"
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Question"
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
tableView.register(AnswerCell.self, forCellReuseIdentifier: cellId)
tableView.register(QuestionHeader.self, forHeaderFooterViewReuseIdentifier: headerId)
tableView.sectionHeaderHeight = 50
tableView.tableFooterView = UIView()
db = Firestore.firestore()
loadData()
}
func loadData() {
db.collection("Quizzes").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()
let header = data["Header"] as? String ?? ""
let optionA = data["OptionA"] as? String ?? ""
let optionB = data["OptionB"] as? String ?? ""
let optionC = data["OptionC"] as? String ?? ""
let optionD = data["OptionD"] as? String ?? ""
let correctAnswer = data["CorrectAnswer"] as? String ?? ""
let newQuiz = Quiz(Header: header, OptionA: optionA, OptionB: optionB, OptionC: optionC, OptionD: optionD, CorrectAnswer: correctAnswer)
self.quizArray.append(newQuiz)
}
self.tableView.reloadData()
}
}
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return quizArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
let quiz = quizArray[indexPath.row]
cell.textLabel?.text = "\(quiz.Header): \(quiz.OptionA): \(quiz.OptionB): \(quiz.OptionC): \(quiz.OptionD): \(quiz.CorrectAnswer)"
return cell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableHeaderFooterView(withIdentifier: headerId)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let controller = ResultsViewController()
navigationController?.pushViewController(controller, animated: true)
}
}
Is it even possible to load in data for the static table view header or should I be looking into another way to format my quiz? 甚至有可能为静态表格视图标题加载数据,还是我应该研究另一种格式化测验的方式? Thanks for any help.
谢谢你的帮助。
declare your header xib with this line 用此行声明您的标题xib
let questionHeaderView = questionHeader().loadNib() as! questionHeader
declare an extension for UIView 声明UIView的扩展
extension UIView {
/** Loads instance from nib with the same name. */
func loadNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nibName = type(of: self).description().components(separatedBy: ".").last!
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as! UIView
}}
then in viewDidload() function set tableview header after set it attributes 然后在viewDidload()函数中设置属性后设置tableview标头
questionHeaderView.textLabel.text = "your text here"
self.tableView.tableHeaderView = questionHeaderView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.