如何使用Swift保存TableView中的行的重新排序

时间:2017-06-25 09:47:25

标签: ios swift uitableview

我已经在几天内搜索了关于如何用Swift保存重新排序TableView的问题的答案?互联网上的这么多教程都展示了如何重新排序,而不是如何使用UserDefaults进行保存。

这是我现在的代码。我错了什么?

请帮我解决一下吗?非常感谢你。

import UIKit

class soundboardTableViewController: UITableViewController {

    var list = ["January","February","March","April","May","June", "July","August","September","October","November", "December"]



    override func viewDidLoad() {
        super.viewDidLoad()


        self.navigationItem.rightBarButtonItem = self.editButtonItem

        let blockItems = UserDefaults.standard.object(forKey:"soundboard")

        if(blockItems != nil) {
            list = blockItems as! [String]
        }
    }


    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

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


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = list[indexPath.row]

        return cell
    }


    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }


    /* Override to support editing the table view.*/
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            list.remove(at: indexPath.row)

            UserDefaults.standard.set(self.list, forKey:"soundboard")
            UserDefaults.standard.synchronize()

            tableView.reloadData()
        }
    }

    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt indexPath: IndexPath, to: IndexPath) {
        let itemToMove = list[indexPath.row]
        list.remove(at: indexPath.row)
        list.insert(itemToMove, at: indexPath.row)

        UserDefaults.standard.set(self.list, forKey:"soundboard")
        UserDefaults.standard.synchronize()
    }

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)

        if(self.isEditing) {

        }
    }


    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }



    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }


}

1 个答案:

答案 0 :(得分:2)

tableView(_:moveRowAt:to:)中的代码有一些错误。你应该把它改成:

override func tableView(_ tableView: UITableView, moveRowAt indexPath: IndexPath, to: IndexPath) {
    let itemToMove = list[indexPath.row]
    list.remove(at: indexPath.row)
    list.insert(itemToMove, at: to.row)

    UserDefaults.standard.set(self.list, forKey:"soundboard")
}

请注意,我删除了UserDefaults.standard.synchronize()。您应该从其他方法中删除它,因为在这种情况下它是无意义的。我还将list.insert(itemToMove, at: indexPath.row)更改为list.insert(itemToMove, at: to.row),因为您需要将其放置到"目的地"索引,而不是从删除它的位置回来。

你的其余代码似乎很好。一旦执行这些更改,它应该正常工作。