2012-03-10 47 views
0

我有被存储在NSUserDefaults的最爱,单收藏存储通过NSDictionary的列表:通过的tableview细胞删除NSUserDefault的对象

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

一切都OK了,但现在我想删除一个单一的最爱与细胞的轻扫。我添加了显示删除刷卡的方法,但我不知道如何从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]; 
} 
+0

看到我在http://stackoverflow.com/questions/9629810/fail-to-addobject-to-nsmutablearray/9629945#comment12242921_9629945 答案它可以帮助你。 – janusbalatbat 2012-03-10 12:56:53

回答

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

Thk,回复。但如何删除单个书签与方法tableView:(UITableView *)tableView commitEditingStyle ?? – Maurizio 2012-03-10 13:08:58

+0

你能解释一下你的意思吗?单个书签 – Shubhank 2012-03-10 13:14:21

+0

对不起,我已经通过你的代码解决了。 THK。如果有帮助,最后 – Maurizio 2012-03-10 13:20:18