2012-03-10 46 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