在Swift / iOS中从UITableView删除行并从NSUserDefaults更新数组的正确方法
从 UITableView
删除行并从NSUserDefaults更新数组的正确方法是什么?
What is the proper way to delete rows from UITableView
and update array from NSUserDefaults?
例如,我从 NSUserDefaults
中读取一个数组,并将其内容提供给 UITableView
,我还允许用户删除 UITableView
中的项目我不确定是何时读写 NSUserDefaults
以便表更新一旦删除行。如您所见,我首先在 viewDidLoad
方法中读取数组,然后在 commitEditingStyle
方法中重新保存它。使用这种方法,删除行后,我的表不会重新加载。
In the following example I'm reading an array from NSUserDefaults
and feeding a UITableView
with its content, I'm also allowing the user to delete items in the UITableView
what I'm not sure is when to read and write to NSUserDefaults
so the table updates as soon as a row is deleted. As you can see I start by reading the array the in the viewDidLoad
method and re-saving it in the commitEditingStyle
method. With this method my table is not reloading when a row is deleted.
override func viewDidLoad() {
super.viewDidLoad()
// Lets assume that an array already exists in NSUserdefaults.
// Reading and filling array with content from NSUserDefaults.
let userDefaults = NSUserDefaults.standardUserDefaults()
var array:Array = userDefaults.objectForKey("myArrayKey") as? [String] ?? [String]()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel!.text = array[indexPath.row]
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
array.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
// Save array to update NSUserDefaults
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(array, forKey: "myArrayKey")
// Should I read from NSUserDefaults here right after saving and then reloadData()?
}
通常如何处理?
谢谢
基本上是正确的,但是如果删除了某些内容,则只应保存用户默认设置。
Basically it's correct, but you should only save in user defaults if something has been deleted.
if editingStyle == UITableViewCellEditingStyle.Delete {
array.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(array, forKey: "myArrayKey")
}
不需要也不建议读回数组。
Reading the array back is not needed and not recommended.
在 cellForRowAtIndexPath 重用该单元格,您需要在Interface Builder中指定标识符。
In cellForRowAtIndexPath
reuse the cell, you need to specify the identifier in Interface Builder.
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
数据源数组必须在上声明
The data source array must be declared on the top level of the class
var array = [String]()
然后在 viewDidLoad
中分配值并重新加载表视图。
then assign the value in viewDidLoad
and reload the table view.
override func viewDidLoad() {
super.viewDidLoad()
let userDefaults = NSUserDefaults.standardUserDefaults()
guard let data = userDefaults.objectForKey("myArrayKey") as? [String] else {
return
}
array = data
tableView.reloadData()
}