我有一個視圖控制器與表視圖,您可以刪除單元格。我有另一個處理稱爲書籤的類,這個類被稱爲BookmarkHandler
。有一些方法可以上傳書籤,獲取整個書籤數組並刪除書籤。下面是類如下:Objective-C NSMutableArray removeObjectAtIndex:崩潰
+ (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];
}
}
所以,當我刪除單元格時,我通過調用deleteBookmark:
從BookmarkHandler
中刪除書籤,並從表格視圖中刪除該行。但有時這條線上有崩潰:
[bookmarkCollection removeObjectAtIndex: indexPath.row];
但沒有崩潰日誌,並且我添加了一個所有異常斷點。
有什麼我做錯了嗎?感謝您的幫助...
什麼調試器告訴你,當你踩過去斷點? – geraldWilliam
@geraldWilliam什麼都沒有!真的很奇怪...... – MCKapur
請注意,Xcode 4/iOS 5中存在一個錯誤,因爲它在所有情況下都無法可靠地產生異常追溯。看到[這個線程](http://stackoverflow.com/questions/8100054/no-exception-stack-trace-in-console-under-xcode-4-2-ios-5)。 –