我已经使用Realm.io实现了ios应用程序的基本示例
我希望能够在我的iOS应用程序中重新排序表行并将订单保存回Realm.领域模型包含position
为此目的而调用的属性.
PS:很抱歉这么多代码.
import UIKit import Realm class Cell: UITableViewCell { var position: Int! init(style: UITableViewCellStyle, reuseIdentifier: String!) { super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier) } } class Language: RLMObject { var title = "" var position = Int() } class ManagerLanguagesController: UITableViewController, UITableViewDelegate, UITableViewDataSource { var array = RLMArray() var notificationToken: RLMNotificationToken? var editButton = UIBarButtonItem() override func viewDidLoad() { super.viewDidLoad() setupUI() notificationToken = RLMRealm.defaultRealm().addNotificationBlock { note, realm in self.reloadData() } reloadData() } override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { return Int(array.count) } func setupUI() { tableView.registerClass(Cell.self, forCellReuseIdentifier: "cell") self.title = "Languages" var addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "add") editButton = UIBarButtonItem(title: "Edit", style: .Plain, target: self, action: "edit") var buttons = [addButton, editButton] self.navigationItem.rightBarButtonItems = buttons } func add() { var addLanguageView:UIViewController = self.storyboard.instantiateViewControllerWithIdentifier("newLanguage") as UIViewController self.navigationController.presentViewController(addLanguageView, animated: true, completion: nil) } func edit () { if tableView.editing { /* FROM THIS POINT I'M PROBABLY DOING SOMETHING WRONG.. IT IS NOT WORKING */ var positionArray = NSMutableArray() let realm = RLMRealm.defaultRealm() var i = 0 for var row = 0; row < tableView.numberOfRowsInSection(0); row++ { var cellPath = NSIndexPath(forRow: row, inSection: 0) var cell:Cell = tableView.cellForRowAtIndexPath(cellPath) as Cell positionArray.addObject(cell.position) } realm.beginWriteTransaction() for row: RLMObject in array { row["position"] = positionArray[i] i++ } realm.commitWriteTransaction() /* -- NOT WORKING END -- */ tableView.setEditing(false, animated: true) editButton.style = UIBarButtonItemStyle.Plain editButton.title = "Edit" } else{ tableView.setEditing(true, animated: true) editButton.title = "Done" editButton.style = UIBarButtonItemStyle.Done } } override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { let cell = tableView!.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as Cell let object = array[UInt(indexPath!.row)] as Language cell.textLabel.text = object.title cell.position = object.position // I have implemented this to be able to retain initial positions for each row and maybe use this when reordering.. return cell } override func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool { return true } override func tableView(tableView: UITableView!, moveRowAtIndexPath sourceIndexPath: NSIndexPath!, toIndexPath destinationIndexPath: NSIndexPath!) { // println("Old index: \(sourceIndexPath.indexAtPosition(sourceIndexPath.length - 1)+1)") // println("New index: \(destinationIndexPath.indexAtPosition(sourceIndexPath.length - 1)+1)") // Maybe something needs to be implemented here instead... } func reloadData() { array = Language.allObjects().arraySortedByProperty("position", ascending: true) tableView.reloadData() } }
提前致谢
您可以将有序数组作为另一个对象的属性来代替使用position属性.这样您就不必保持最新位置,而是根据需要排列对象:
class Language: RLMObject { dynamic var title = "" } class LanguageList: RLMObject { dynamic var languages = RLMArray(objectClassName: "Language") } class ManagerLanguagesController: UITableViewController, UITableViewDelegate, UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() // create our list var realm = RLMRealm.defaultRealm() realm.beginWriteTransaction() realm.addObject(LanguageList()) realm.commitWriteTransaction() ... } // helper to get the RLMArray of languages in our list func array() -> RLMArray { return (LanguageList.allObjects().firstObject() as LanguageList).languages } override func tableView(tableView: UITableView!, moveRowAtIndexPath sourceIndexPath: NSIndexPath!, toIndexPath destinationIndexPath: NSIndexPath!) { var languages = array() var object = languages.objectAtIndex(UInt(sourceIndexPath.row)) as Language var realm = RLMRealm.defaultRealm() realm.beginWriteTransaction() languages.removeObjectAtIndex(UInt(sourceIndexPath.row)) languages.insertObject(object, atIndex: UInt(destinationIndexPath.row)) realm.commitWriteTransaction() } ... }