gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:52:10 24 4
gpt4 key购买 nike

我有几个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com