【问题标题】:UITableView with Multiple Sections using Realm and Swift使用 Realm 和 Swift 的具有多个部分的 UITableView
【发布时间】:2016-08-02 19:28:11
【问题描述】:

好的,所以我找到了很多关于 UITableView 和多个部分的信息,但是,它们总是包含字符串、数组、静态数据、Obj-C 或其他我无法转化为我的情况的东西,主要是因为我对开发应用程序完全陌生。非常感谢任何帮助,因为我已经一个多月来尝试不同的方法但没有成功。

所以我有多个具有以下属性的 Dog 对象:

class Dog: Object {
    dynamic var name = ""
    dynamic var race = ""
    dynamic var age = 0
    dynamic var owner = ""
    dynamic var dogID = ""

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

在我的 ViewController 文件中,我有以下代码(我删除了不相关的行):

let realm = try! Realm()
var dogResults : Results<Dog>?

override func viewDidLoad() {
    super.viewDidLoad()

    self.dogResults = realm.objects(Dog.self)

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dogResults!.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    let dog = self.dogResults![indexPath.row]
    cell.textLabel?.text = dog.name
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        // ?
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // ?
}

我想通过 Dog 对象的“race”属性来组织这些部分,但是我很难做到这一点。

我看到了一些示例,他们在这些部分中使用了“if”语句,我已经尝试过,但无法获得正确的结果,但是,我想要在其他示例中看到的更简洁的方法,使用:

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

我进行了各种尝试,但作为一个 Realm DB 和 swift 我在遵循大多数示例时遇到了问题。

感谢您的宝贵时间。

【问题讨论】:

    标签: ios swift uitableview realm


    【解决方案1】:

    这里有一些示例代码,完全符合您的要求:

    import UIKit
    import RealmSwift
    
    class Dog: Object {
        dynamic var name = ""
        dynamic var race = ""
        dynamic var age = 0
        dynamic var owner = ""
        dynamic var dogID = ""
    
        override static func primaryKey() -> String? {
            return "dogID"
        }
    
        convenience init(name: String, race: String, dogID: String) {
            self.init()
            self.name = name
            self.race = race
            self.dogID = dogID
        }
    }
    
    class TableViewController: UITableViewController {
        let items = try! Realm().objects(Dog.self).sorted(["race", "name"])
        var sectionNames: [String] {
            return Set(items.valueForKeyPath("race") as! [String]).sort()
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    
            let realm = try! Realm()
            if realm.isEmpty {
                try! realm.write {
                    realm.add(Dog(name: "Bailey", race: "Golden Retrievers", dogID: "0"))
                    realm.add(Dog(name: "Bella", race: "German Shepherds", dogID: "1"))
                    realm.add(Dog(name: "Max", race: "Bulldogs", dogID: "2"))
                    realm.add(Dog(name: "Lucy", race: "Yorkshire Terriers", dogID: "3"))
                    realm.add(Dog(name: "Charlie", race: "Bulldogs", dogID: "4"))
                    realm.add(Dog(name: "Molly", race: "German Shepherds", dogID: "5"))
                    realm.add(Dog(name: "Buddy", race: "German Shepherds", dogID: "6"))
                    realm.add(Dog(name: "Daisy", race: "Siberian Huskies", dogID: "7"))
                }
            }
        }
    
        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return sectionNames.count
        }
    
        override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return sectionNames[section]
        }
    
        override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
            return items.filter("race == %@", sectionNames[section]).count
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
            cell.textLabel?.text = items.filter("race == %@", sectionNames[indexPath.section])[indexPath.row].name
            return cell
        }
    }
    

    看起来像这样:

    【讨论】:

    • 谢谢 jpsim!像魅力一样工作!
    • 这对我也很有效。非常感谢!我在您的文档和 SO 上找到的 Realm 支持给我留下了深刻的印象。
    • @jpsim 是否可以将其与领域收集通知结合起来?领域文档中的示例显示了如何仅基于通知更新具有单个部分的表格视图...谢谢。
    • 是的,这是可能的,但很棘手,我没有任何示例代码可以分享。如果您找到了一种优雅的方法,请分享。
    • @MikePollard 是的,有可能。我们为不同的部分设置了收集通知,但在我们的例子中,我们必须在一个部分中显示 3 个项目。
    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多