ios - 如何从多个 Realm 模型创建一个 tableview?

标签 ios swift uitableview realm

我有几个 Realm 模型,它们看起来像这样。

class Dates: Object {
dynamic var date = NSDate()
let people = List<PEOPLE>()
let animals = List<ANIMALS>()
let objects = List<OBJECTS>() }

是否可以为特定日期创建单个表格 View ,并为每个模型(人、动物、物体)创建相应的部分?对代码的外观有什么想法吗?

它可能看起来像这样。

enter image description here

最佳答案

当然可以。

  • 创建一个 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/

相关文章:

ios - 尝试共享 View 时应用程序崩溃

ios - UICollectionView:从自定义单元格中更改 sizeForItemAtIndexPath

ios - 如何在 iOS 中将解析读取图像与屏幕显示同步

iphone - 如何在 UItableview 下使用 UItableview 创建 Accordion ?

ios - 单击 UITableViewCell 按钮时传递到 didSelectRowAtIndexPath

ios - 看不到属性错误

ios - [UIAlert show] 什么时候真正发生?

ios - 是否建议在完成与它们的工作后销毁 UIViewController

c# - 为什么我在 UITableView 中有一个空的(但有效的)表索引带?

ios - 更改 UIView 的转换不会影响约束