我有几个 Realm 模型,它们看起来像这样。
class Dates: Object {
dynamic var date = NSDate()
let people = List<PEOPLE>()
let animals = List<ANIMALS>()
let objects = List<OBJECTS>() }
是否可以为特定日期创建单个表格 View ,并为每个模型(人、动物、物体)创建相应的部分?对代码的外观有什么想法吗?
它可能看起来像这样。
最佳答案
当然可以。
- 创建一个 UITableViewController
- 实现委托(delegate)方法
像那样:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return NUMBER_OF_PEOPLE
} else if section == 1 {
return NUMBER_OF_ANIMALS
}else if section == 2 {
return NUMBER_OF_OBJECTS
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "people"
} else if section == 1 {
return "animals"
}else if section == 2 {
return "objects"
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
if indexPath.section == 0 {
cell.textLabel.text = people[indexPath.row]
} else if indexPath.section == 1 {
cell.textLabel.text = animals[indexPath.row]
}else if indexPath.section == 2 {
cell.textLabel.text = objects[indexPath.row]
}
return cell
}
关于ios - 如何从多个 Realm 模型创建一个 tableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36147801/