【问题标题】:How do I load Static UITableView Headers into my app from Firebase Firestore?如何从 Firebase Firestore 将静态 UITableView 标头加载到我的应用程序中?
【发布时间】:2019-12-15 16:18:20
【问题描述】:

我设置了一个 firebase firestore 数据库来存储我的 iOS 应用程序的测验数据。我的测验数据应该加载到分组的静态表格视图中,部分标题是问题,部分中的每个单元格都是答案选择。我的问题是我不知道如何将数据加载到标题中。

https://imgur.com/a/SpZhtUL

这就是我的测验表视图现在的样子。加载的数据实际上应该是“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)
    }
}

是否甚至可以为静态表格视图标题加载数据,或者我应该寻找另一种方式来格式化我的测验?感谢您的帮助。

【问题讨论】:

    标签: ios firebase uitableview google-cloud-firestore


    【解决方案1】:

    用这一行声明你的标题 xib

    let questionHeaderView = questionHeader().loadNib() as! questionHeader
    

    为 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
    }}
    

    然后在viewDidload()函数中设置tableview header后设置属性

     questionHeaderView.textLabel.text = "your text here"
     self.tableView.tableHeaderView = questionHeaderView
    

    【讨论】:

    • 感谢您的回复。我将扩展添加到我的 QuestionHeader.swift 类(我认为这很好?)。我将另外两件事添加到我的 QuestionTableViewController 中。当我运行时,我得到一个 NSInternalInconsistencyError: Could not load nib in bundle 'NSBundle... QuestionHeader"。我尝试将扩展移动到我的 QuestionTableViewController 但这给了我相同的结果。有什么想法吗?
    猜你喜欢
    • 2021-02-02
    • 2021-12-26
    • 2018-07-10
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2021-11-27
    相关资源
    最近更新 更多