【问题标题】:How to create one tableview from several Realm models?如何从多个 Realm 模型创建一个 tableview?
【发布时间】:2016-03-22 06:45:19
【问题描述】:

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

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

是否可以为特定日期创建单个表格视图,并为每个模型(人、动物、对象)提供相应的部分?对代码的外观有何想法?

它可能看起来像这样。

【问题讨论】:

    标签: ios swift uitableview realm


    【解决方案1】:

    当然可以。

    • 创建一个 UITableViewController
    • 实现委托方法

    这样:

    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
    }
    

    【讨论】:

    • 很好的回复!它有效,谢谢!我唯一需要改变的是在 numberOfRowsInSection 和 titleForHeaderInSection 中相应地添加 (return 1) 和 (return " ")。
    • 哦,当然..我忘记了这些函数中的默认返回,因为我是直接在这里写的。
    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    相关资源
    最近更新 更多