override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
allNotes.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
//这是注释类
var allNotes:[Note] = []
var currentNoteIndex:Int = -1
var noteTable:UITableView?
let kAllNotes:String = "notes"
class Note:NSObject {
var date:String
var note:String
override init() {
date = NSDate().description
note = ""
}
func dictionary() -> NSDictionary {
return ["note":note, "date": date]
}
class func savedNotes() {
var aDictionary:[NSDictionary] = []
for var i:Int = 0; i < allNotes.count; i++ {
aDictionary.append(allNotes[i].dictionary())
}
NSUserDefaults.standardUserDefaults().setObject(aDictionary, forKey: kAllNotes)
}
class func loadNotes() {
var defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
var savedData:[NSDictionary]? = defaults.objectForKey(kAllNotes) as? [NSDictionary]
if let data:[NSDictionary] = savedData {
for var i:Int = 0; i < data.count; i++ {
var n:Note = Note()
n.setValuesForKeysWithDictionary(data[i] as [NSObject : AnyObject])
allNotes.append(n)
}
}
}
}
这是一个函数,但它不会永久地删除数组上的项,而是从表视图中临时删除。当重新启动模拟器时,仍然表视图显示已删除的项
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
allNotes.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
NSUserDefaults.standardUserDefaults().setObject(allNotes, forKey: kAllNotes)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
当我像这样更新任何尝试删除数组项时,显示一个线程错误
是的,它只会在运行时从您的allNotes
数组中删除,因为您不会从内存中删除您的allNotes
数组的获取值。
例如,如果你从内存中读取plist文件中的数组,那么你必须从plist文件中删除该项。
所以当你重新启动你的应用程序,你的allNotes
数组将不会得到删除的项目从你的plist。