我有一个简单的tableView在我的应用程序中运行,带有重新排序tableViewCell函数。当我尝试重新排序cell.reordering works.but当我关闭应用程序并重新打开它时,单元格顺序恢复正常。我想知道如何保存swift.my中的新订单单元格如下简单的代码,没有什么特别的简单的tableview ....
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var tableData: [String] = ["Argentina", "Brazil", "Australia","Austria", "Chile", "Denmark","Colombia", "Denmark", "Czech Republic","Egypt", "Ireland", "Germany","Hong Kong", "Netherlands", "Mexico","Poland", "Switzerland", "Panama","United Arab Emirates", "Belgium", "Armenia"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.reloadData()
}
// Toggle editing mode on/off
@IBAction func reorderItems(sender: AnyObject) {
if(tableView.editing == true) {
tableView.setEditing(false, animated: true)
} else {
tableView.setEditing(true, animated: true)
}
}
// Set editing mode to show 3 bars on right hand side
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.None
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
var itemToMove = tableData[sourceIndexPath.row]
tableData.removeAtIndex(sourceIndexPath.row)
tableData.insert(itemToMove, atIndex: destinationIndexPath.row)
self.tableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.tableData[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("Row \(indexPath.row) selected")
}
}
感谢...
答案 0 :(得分:1)
要保留自定义订单,您需要将数组保存为用户默认值
虽然不建议将NSUserDefaults
用于大型数据集,但请在此示例代码中使用它。
AppDelegate
在didFinishLaunchingWithOptions
中添加3行以注册默认值。
let countries = ["Argentina", "Brazil", "Australia","Austria", "Chile", "Denmark","Colombia", "Denmark", "Czech Republic","Egypt", "Ireland", "Germany","Hong Kong", "Netherlands", "Mexico","Poland", "Switzerland", "Panama","United Arab Emirates", "Belgium", "Armenia"]
let defaultValues = ["countries" : countries]
NSUserDefaults.standardUserDefaults().registerDefaults(defaultValues)
<强>的ViewController 强>
将tableData
声明为空数组。
var tableData = [String]()
在viewWillAppear
添加
let defaults = NSUserDefaults.standardUserDefaults()
tableData = defaults.objectForKey("countries") as! [String]
tableView.reloadData()
将方法reorderItems
替换为
@IBAction func reorderItems(sender: AnyObject) {
if tableView.editing == true {
tableView.setEditing(false, animated: true)
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(tableData , forKey: "countries")
} else {
tableView.setEditing(true, animated: true)
}
}