简体   繁体   English

如何从Firebase Firestore将静态UITableView标头加载到我的应用程序中?

[英]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.

相关问题 如何将数据从NSMutableDictionary加载到UITableView - How do I load the data from my NSMutableDictionary to my UITableView 如何将我的 Firebase Firestore 项目从测试模式转换为 iOS 项目的生产模式? - How do I turn my Firebase Firestore project from test mode into Production mode for my iOS project? 如何使用来自sqlite的数据加载UITableView? - How do I use data from sqlite to load a UITableView? 如何从 Firebase Storage 下载 USDZ 模型并将它们加载到我的 iOS 应用上的 ARQuickLook 中? - How can I download USDZ models from Firebase Storage and load them into ARQuickLook on my iOS app? 如何用JSON数组中的数据填充UITableView? - How do I populate my UITableView with data from JSON array? Firebase:如何在应用检查中注册我的 Flutter 应用? - Firebase: How do I register my Flutter app in app check? 如何在UITableView上不显示“…”? - How do I not show the “…” on my UITableView? 如何从Firebase加载一系列refs? - How do I load an array of refs from Firebase? 如何以正确的方式从 firebase 数据库加载图像 - How do I load image from firebase database the correct way 如何为静态uitableview单元添加活版效果 - How do I add letterpress effect for static uitableview cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM