I want to let the user of my App delete items from the NSUserDefaults out of a tableView by swiping left on the Cell and press "delete". I know both function but I don't know how to bring them together in the right was so that the "remove swipe" affects the variable in NSUserDefaults. Maybe someone can help me out. Thank you.
This function should allow the User to access the delete Button in a tableView:
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete)
}
and the function should remove items from NSUserDefaults:
@IBAction func deleteItem(sender: AnyObject) {
var userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
var itemListArray:NSMutableArray = userDefaults.objectForKey("itemList") as NSMutableArray
var mutableItemList:NSMutableArray = NSMutableArray()
for dict:AnyObject in itemListArray{
mutableItemList.addObject(dict as NSDictionary)
}
mutableItemList.removeObject(toDoData)
userDefaults.removeObjectForKey("itemList")
userDefaults.setObject(mutableItemList, forKey: "itemList")
userDefaults.synchronize()
}