【发布时间】:2015-01-28 20:05:51
【问题描述】:
我想让我的应用程序的用户通过在单元格上向左滑动并按“删除”从 tableView 中删除 NSUserDefaults 中的项目。我知道这两个功能,但我不知道如何将它们放在右边,以便“删除滑动”影响 NSUserDefaults 中的变量。也许有人可以帮助我。谢谢。
此函数应允许用户访问 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)
}
并且该函数应该从 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()
}
【问题讨论】:
标签: ios xcode uitableview swift nsuserdefaults