在 Swift / iOS 中从 UITableView 删除行并从 NSUserDefaults 更新数组的正确方法
The proper way to delete rows from UITableView and update array from NSUserDefaults in Swift / iOS
从 UITableView
中删除行并从 NSUserDefaults 更新数组的正确方法是什么?
在下面的示例中,我从 NSUserDefaults
读取一个数组并将其内容提供给 UITableView
,我还允许用户删除 UITableView
中的项目我不确定何时读取和写入 NSUserDefaults
,以便 table 在删除一行后立即更新。如您所见,我首先在 viewDidLoad
方法中读取数组并将其重新保存在 commitEditingStyle
方法中。使用此方法,我的 table 不会在删除行时重新加载。
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()?
}
这通常是如何处理的?
谢谢
基本上是正确的,但如果某些内容已被删除,您应该只保存在用户默认值中。
if editingStyle == .delete {
array.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
let userDefaults = UserDefaults.standard
userDefaults.set(array, forKey: "myArrayKey")
}
不需要也不推荐读回数组。
在cellForRowAtIndexPath
重用单元格时,需要在Interface Builder中指定标识符。
let cell = tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath)
必须在 class
的顶层声明数据源数组
var array = [String]()
然后在 viewDidLoad
中分配值并重新加载 table 视图。
override func viewDidLoad() {
super.viewDidLoad()
let userDefaults = UserDefaults.standard
guard let data = userDefaults.array(forKey: "myArrayKey") as? [String] else {
return
}
array = data
tableView.reloadData()
}
从 UITableView
中删除行并从 NSUserDefaults 更新数组的正确方法是什么?
在下面的示例中,我从 NSUserDefaults
读取一个数组并将其内容提供给 UITableView
,我还允许用户删除 UITableView
中的项目我不确定何时读取和写入 NSUserDefaults
,以便 table 在删除一行后立即更新。如您所见,我首先在 viewDidLoad
方法中读取数组并将其重新保存在 commitEditingStyle
方法中。使用此方法,我的 table 不会在删除行时重新加载。
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()?
}
这通常是如何处理的?
谢谢
基本上是正确的,但如果某些内容已被删除,您应该只保存在用户默认值中。
if editingStyle == .delete {
array.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
let userDefaults = UserDefaults.standard
userDefaults.set(array, forKey: "myArrayKey")
}
不需要也不推荐读回数组。
在cellForRowAtIndexPath
重用单元格时,需要在Interface Builder中指定标识符。
let cell = tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath)
必须在 class
的顶层声明数据源数组var array = [String]()
然后在 viewDidLoad
中分配值并重新加载 table 视图。
override func viewDidLoad() {
super.viewDidLoad()
let userDefaults = UserDefaults.standard
guard let data = userDefaults.array(forKey: "myArrayKey") as? [String] else {
return
}
array = data
tableView.reloadData()
}