2016-07-25 57 views
1

我已经通过Realm的GroupedTableView示例提供的示例构建了我的二维数组数据模型。但是,我试图实现这个功能来重新排序我的tasksByPriority数组中的任务,并且我不确定如何去插入特定的数组。目前,Realm Results数组正在按dateCreated进行排序,我只知道如何在Realm对象的末尾添加新任务给Realm,而不是在特定索引处。如何在Realm数组中的特定索引处插入对象?

import UIKit 
import MGSwipeTableCell 
import RealmSwift 
import Realm 
import AEAccordion 

class TasksTableViewController: UITableViewController { 

    var realm: Realm! 
    var notificationToken: NotificationToken? 
    var priorityIndexes = [0, 1, 2, 3] 
    var priorityTitles = ["Urgent | Important", "Urgent | Not Important", "Not Urgent | Important", "Not Urgent | Not  Important"] 
    var tasksByPriority = [Results<Task>]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     realm = try! Realm() 

     notificationToken = realm.addNotificationBlock { [unowned self] note, realm in 
      self.tableView.reloadData() 
     } 

     for priority in priorityIndexes { 
      let unsortedObjects = realm.objects(Task.self).filter("priorityIndex == \(priority)") 
      let sortedObjects = unsortedObjects.sorted("dateCreated", ascending: true) 
      tasksByPriority.append(sortedObjects) 
     } 
    } 


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return priorityIndexes.count 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return Int(tasksByPriority[section].count) 
    } 


    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return priorityTitles[section] 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("taskTableViewCell", forIndexPath: indexPath) as! TaskTableViewCell 

     // Configure the cell... 
     let task = self.taskForIndexPath(indexPath) 

      //configure right buttons 
     cell.rightButtons = [MGSwipeButton(title: "", icon: UIImage(named:"deleteTask.png"), backgroundColor: UIColor(red: 0, green: 0, blue: 0, alpha: 0), callback: { 
       (sender: MGSwipeTableCell!) -> Bool in 
       RealmHelper.deleteTask(self.taskForIndexPath(indexPath)!) 
       return true 
     })] 

     return cell 
    } 

    func taskForIndexPath(indexPath: NSIndexPath) -> Task? { 
     return tasksByPriority[indexPath.section][indexPath.row] 
    } 


    // MARK: Segues 

    @IBAction func unwindToTasksTableViewController(sender: UIStoryboardSegue) { 
     if let sourceViewController = sender.sourceViewController as? DisplayTaskViewController, task = sourceViewController.task, priorityIndex = sourceViewController.priorityIndex { 
      if let selectedIndexPath = tableView.indexPathForSelectedRow { 
       // Update an existing task. 
       if selectedIndexPath.section == priorityIndex { // not changing priority 
        RealmHelper.updateTask(taskForIndexPath(selectedIndexPath)!, newTask: task) 
       } 

       else { // changing priority 
        RealmHelper.addTask(task) 
        RealmHelper.deleteTask(taskForIndexPath(selectedIndexPath)!) 
       } 
      } 
      else { 
       RealmHelper.addTask(task) 
      } 
     } 

    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if let identifier = segue.identifier { 

      if identifier == "editTaskDetailsSegue" { 
       let displayTaskViewController = segue.destinationViewController as! DisplayTaskViewController 

       // set task of DisplayTaskViewController to task tapped on 
       if let selectedTaskCell = sender as? TaskTableViewCell { 
        let indexPath = tableView.indexPathForCell(selectedTaskCell)! 
        let selectedTask = taskForIndexPath(indexPath) 
        displayTaskViewController.task = selectedTask 
        print("Task completed: \(selectedTask!.completed)") 
        displayTaskViewController.priorityIndex = indexPath.section 
        displayTaskViewController.completed = (selectedTask?.completed)! 
       } 
      } 

      else if identifier == "addNewTaskSegue" { 
      } 
     } 
    } 
} 

境界助手类

import Foundation 
import RealmSwift 

class RealmHelper { 

static func addTask(task: Task) { 
    let realm = try! Realm() 
    try! realm.write() { 
     realm.add(task) 
    } 
} 



static func deleteTask(task: Task) { 
    let realm = try! Realm() 
    try! realm.write() { 
     realm.delete(task) 
    } 
} 

static func updateTask(taskToBeUpdated: Task, newTask: Task) { 
    let realm = try! Realm() 
    try! realm.write() { 
     taskToBeUpdated.text = newTask.text 
     taskToBeUpdated.details = newTask.details 
     taskToBeUpdated.dueDate = newTask.dueDate 
     taskToBeUpdated.completed = newTask.completed 
    } 
} 
} 
+1

所有400行代码是否真的与您的问题相关?你能否削减代码以更好地突出与你的问题相关的部分? – bdash

+0

感谢您花时间回答我的问题。我现在意识到,在我的类中留下所有不必要的代码是粗心的,我已经删除它,只留下与我的Realm阵列数据模型相关的方法和变量。基本上我不确定tasksByPriority结果数组是如何“神奇地”排序超时我删除或添加一个任务,当我只在viewDidLoad()设置过滤和排序。这导致我的主要问题是,如果向Realm“添加”任务不允许索引,我可以将任务插入某个索引(用于重新排序任务)。 – Reservations

回答

2

TL; DR在境界

对象是无序的。如果您确实想要排列对象,则可以使用List<T>类型。 A List可以进行突变,可以是insert(_:atIndex:),removeAtIndex(_:)等。这里是List Class Reference,你可以在这里找到如何使用它。

+0

谢谢!这个伎俩。作为参考,我在ReactKit文件夹中的示例Realm swift代码的TableViewController类之后对代码进行了重新编译:https://github.com/realm/realm-cocoa – Reservations

相关问题