1

我有一个带有表格视图的视图控制器,您可以在其中删除单元格。我有另一个处理称为书签的类,该类称为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 断点。

有什么我做错了吗?感谢帮助...

4

2 回答 2

4

问题在这里:

NSMutableArray *bookmarkCollection = [[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"];

NSUserDefaults 不保存可变数组,它只是将它保存为 NSArray。
所以获取它的可变副本:

NSMutableArray *bookmarkCollection = [[[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"] mutableCopy];
于 2012-09-27T01:29:52.137 回答
1

您应该检查 indexPath.row 以确保它没有越界。在此之后,您可能会找到原因。

if (indexPath.row>=0 && indexPath.row<bookmarkCollection.count) {
    [bookmarkCollection removeObjectAtIndex: indexPath.row];
} else {
    NSLog(@"indexPath.row is out of boundry of bookmarkCellection size: %d", bookmarkCollection.count);
}
于 2012-09-27T01:09:14.727 回答