我有一个带有表格视图的视图控制器,您可以在其中删除单元格。我有另一个处理称为书签的类,该类称为BookmarkHandler
. 有一些方法可以上传书签、获取整个书签数组和删除书签。下面是这个类:
+ (NSMutableArray *)bookmarkCollection {
NSMutableArray *bookmarkCollection = [[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"];
if (!bookmarkCollection) {
bookmarkCollection = [[NSMutableArray alloc] init];
}
return bookmarkCollection;
}
+ (void)deleteBookmark: (NSIndexPath *)indexPath {
NSMutableArray *bookmarkCollection = [[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"];
[bookmarkCollection removeObjectAtIndex: indexPath.row];
[[NSUserDefaults standardUserDefaults] setObject:bookmarkCollection forKey: @"bookmarks"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (void)uploadBookmark:(NSDictionary *)singleBookmark {
NSMutableArray *bookmarkCollection = [[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"];
if (!bookmarkCollection) {
bookmarkCollection = [[NSMutableArray alloc] init];
}
NSMutableDictionary *bookmark1 = [[NSMutableDictionary alloc] initWithDictionary: singleBookmark];
NSMutableDictionary *bookmark2 = [[NSMutableDictionary alloc] initWithDictionary: singleBookmark];
NSNumber *number1 = [[NSNumber alloc] initWithInt: 1];
NSNumber *number2 = [[NSNumber alloc] initWithInt: 2];
[bookmark1 setObject:number1 forKey: @"bookmarkTag"];
[bookmark2 setObject:number2 forKey: @"bookmarkTag"];
[bookmarkCollection addObject: bookmark1];
[bookmarkCollection addObject: bookmark2];
[[NSUserDefaults standardUserDefaults] setObject:bookmarkCollection forKey: @"bookmarks"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
书签集合是一个可变数组,由具有名称和日期对象/键的字典填充。这些名称和日期是填充另一个视图控制器中表格视图单元格标题的内容。表格视图中的单元格数量由[[BookmarkHandler bookmarkCollection] count];
在另一个视图控制器中,您可以删除表格视图单元格,所以我要做的是实现委托方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[BookmarkHandler deleteBookmark: indexPath];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
因此,当我删除一个单元格时,我BookmarkHandler
通过调用deleteBookmark:
从表视图中删除该行来删除一个书签。但有时这条线上会出现崩溃:
[bookmarkCollection removeObjectAtIndex: indexPath.row];
但是没有崩溃日志,我添加了一个 All Exceptions 断点。
有什么我做错了吗?感谢帮助...