3

Realm.io を使用して iOS アプリの基本的な例を実装しました。

iOS アプリでテーブルの行を並べ替えて、その順序を Realm に保存できるようにしたいと考えています。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()
    }

}

前もって感謝します

4

2 に答える 2

0

これは、Swift 2.2でレルムを使用してテーブルビューで行を移動するために機能します:

func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
    let aux = TimesHome.mutableCopy() as! NSMutableArray
    let itemToMove = aux[fromIndexPath.row]
    let realm = try! Realm()
    realm.beginWrite()
    aux.removeObjectAtIndex(fromIndexPath.row)
    aux.insertObject(itemToMove, atIndex: toIndexPath.row)
    try! realm.commitWrite()

    TimesHome = aux

    let times = realm.objects(ParciaisTimes)
    if times.count  > 0 {
        for tm in times {
            for i in 1...aux.count {
                if aux[i-1].valueForKey("time_id") as! Int == tm.time_id {
                    realm.beginWrite()
                    tm.ordem = i
                    try! realm.commitWrite()
                }
            }
        }
    }
}
于 2016-06-18T17:25:13.653 に答える