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 which I am unable to translate to my situation, mainly because I am completely new to developing apps. Any help is greatly appreciated since it has been a little over a month that I have been trying different approaches without success.
So I have multiple Dog objects with the following properties:
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"
}
}
And in my ViewController file I have the following code (I removed the irrelevant lines):
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 {
// ?
}
I would like to organize the sections by the "race" property from the Dog object, however I am having a very difficult time achieving this.
I saw a few examples where they use the "if" statement for the sections, I have tried that and was not able to get the proper results, however, I would like the cleaner approach I have seen in some other examples, using:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
I have made various attempts, but being a Realm DB and swift I am having problems following most of the examples.
Thank you for your time.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…