UITableView与多个部分使用Realm和Swift

好吧,所以我发现了很多关于UITableView和多个部分的信息,然而,他们总是使用string,数组,静态数据,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 { // ? } 

我想组织狗对象的“种族”属性部分,但我很难实现这一点。

我看了几个例子,他们使用了“if”语句,但是我试过了,并没有得到正确的结果,但是我希望在其他一些例子中看到更清晰的方法:

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

我做了各种各样的尝试,但作为一个Realm数据库和迅速我遇到了大多数的例子的问题。

感谢您的时间。

以下是一些示例代码,它完全符合您的要求:

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

看起来像这样:

截图