UITableView with Multiple Sections using Realm and Swift

前端 未结 1 1956
自闭症患者
自闭症患者 2020-11-29 02:44

Ok, so I found a lot of information regarding UITableView and multiple sections, however, they are always with strings, arrays, static data, Obj-C or something else with whi

相关标签:
1条回答
  • 2020-11-29 03:13

    Here's some sample code that does exactly what you're looking for:

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

    Which looks like this:

    0 讨论(0)
提交回复
热议问题