通过tableview单元格删除NSUserDefault的对象 [英] Remove object of NSUserDefault by tableview cell

查看:34
本文介绍了通过tableview单元格删除NSUserDefault的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of favorites that are stored in the NSUserDefaults, single favorited are stored by 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中删除单个书签.

Everything is ok, but now I want to delete a single favorite with the swipe of a cell. I have add the method for show the the delete swipe but I do not know how to remove the single bookmark from 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];

这篇关于通过tableview单元格删除NSUserDefault的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆