【问题标题】:Use Realm Query Results as UITableView Section Headers使用 Realm 查询结果作为 UITableView 节标题
【发布时间】:2020-01-31 08:23:27
【问题描述】:

我正在尝试将 Realm 查询的结果用作 UITableView 中的部分标题。

领域类:

class Person: Object {
    @objc dynamic var personId = UUID().uuidString
    @objc dynamic var firstName: String = ""
    @objc dynamic var surname: String = ""
    @objc dynamic var mobileNumber: Int = 0
    @objc dynamic var password: String = ""

    override static func primaryKey() -> String? {
        return "personId"
    }
}

class Category: Object {
    @objc dynamic var categoryId = UUID().uuidString
    @objc dynamic var person: Person?
    @objc dynamic var categoryName: String = ""
    let categoryContent = List<String>()

    override static func primaryKey() -> String? {
        return "categoryId"
    }
}

我的代码:

class HomeController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let realm = try! Realm()
    var itemsInSections: Array<Array<String>> = [["1A"], ["2A"], ["3A"], ["4A"], ["5A"], ["6A"], ["7A"], ["8A"], ["9A"], ["10A"]]  //Test content to figure out later

    @IBOutlet weak var tableDetails: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableDetails.dataSource = self
        tableDetails.delegate = self
    }



    func numberOfSections(in tableView: UITableView) -> Int {
        return getCategoryNames().count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemsInSections[section].count
    }

    private func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> [String] {
        return getCategoryNames()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath)
        let text = self.itemsInSections[indexPath.section][indexPath.row]

        cell.textLabel!.text = text

        return cell
    }

    func getCategoryNames() -> [String] {
        let categoryNames = realm.objects(Category.self).filter("person.mobileNumber == %@", mobileNumber).map({$0.categoryName})
        return Array(categoryNames)
    }

}

节数完美,但节标题为空白。

如果我添加:

print(Array(categoryNames))

对于 getCategoryNames 函数,它多次返回 ["Category 1", "Category 2", "Category 3", "Category 4"]。这似乎是节标题所需的字符串的正确格式,但表标题未显示。

【问题讨论】:

    标签: ios swift xcode uitableview realm


    【解决方案1】:

    试试这个:

    private func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let nameArr = getCategoryNames()
        return nameArr[section]
    }
    

    函数: func tableView(_ tableView: UITableView, titleForHeaderInSection 部分:Int) -> String?所以你需要返回特定部分的字符串。

    【讨论】:

    • 还没有。我在 if let nameArr 行中收到“条件绑定的初始化程序必须具有可选类型,而不是 '[String]'”错误。我的 getCategoryNames() 正在返回 [String]
    • 您是否从函数中删除了 if 语句?只需清理您的代码并运行您的应用程序一次。
    • 现在我只需要在添加新类别时更新我的​​表......
    • 添加新类别时只需重新加载表格视图。 tableView.reload()
    • 添加新类别是在不同的视图控制器中...我可以在 ViewWillAppear 或类似的方法中进行吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多