ios - 将 Realm 查询结果用作UITableView节标题

标签 ios swift xcode uitableview realm

我试图将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”]。这似乎是节标题所需的字符串的正确格式,但表标题未显示。

最佳答案

尝试这个:

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

功能:func tableView(_ tableView:UITableView,
titleForHeaderInSection部分:Int)->字符串?因此您需要为特定部分返回字符串。

关于ios - 将 Realm 查询结果用作UITableView节标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60000245/

相关文章:

ios - 应用被拒绝,但我不使用 UDID

ios - 从 UITableViewCell 调用 UIButton 操作

ios - 删除项目后 CollectionView 崩溃

iphone - 单个对象,多个 IBOutlets

ios - 选择时更改 imageView 的tintColor 不起作用

ios - iPhone 照片应用程序如何如此快速流畅地加载这么多图像?

ios - "scan credit card"功能和 UIWebView 或 WKWebView

ios - Swift:将 [String] 拆分为具有给定子数组大小的 [[String]] 的正确方法是什么?

iphone - 如何强制 iOS 立即更改背景颜色?

ios - 如何减少 reloadData 的故障(视觉上)?