通过tableview单元删除NSUserDefault的对象

时间:2012-03-10 12:27:23

标签: iphone objective-c ios nsuserdefaults

我有一个存储在NSUserDefaults中的收藏夹列表,单个收藏夹由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];
}

1 个答案:

答案 0 :(得分:1)

    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];