按表视图单元格删除NSUserDefault的对象



我有一个存储在NSUserDefault中的收藏夹列表,单个收藏夹由NSDictionary存储:

NSUserDefaults *userPref = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *bookmark = [NSMutableDictionary new];
[bookmark setValue:author.text forKey:@"author"];
[bookmark setValue:book.text forKey:@"book"];
[bookmarks addObject:bookmark];
[userPref setObject:bookmarks forKey:@"bookmarks"];
[userPref synchronize];

一切都很好,但现在我想用滑动一个单元格删除一个收藏夹。我已经添加了显示删除滑动的方法,但我不知道如何从nsuserdefaults中删除单个书签。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
[self.listBookamrks removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                 withRowAnimation:UITableViewRowAnimationFade];
}
    NSUserDefaults *userPref = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *storedBookMark = [[userPref ObjectForKey:@"bookmarks"]mutable copy];
// If you want to remove an object from the dictionary 
[storedBookMark removeObjectForKey:@"Your KEy"];
// if you want to empty the dictionary 
    [storedBookMark removeAllObjects];

    [userPref setObject: storedBookMark forKey:@"bookmarks"];
    [userPref synchronize];
// if you want to store nil // go back to default state ..

    [userPref setNilValueForKey:@"bookmarks];

最新更新