我的应用程序有一个包含部分的表视图,从核心数据实体填充。我已经包含了一个搜索栏。使用搜索栏时,搜索结果对象将显示在新部分下。请参阅两个屏幕截图,首先是带有节的普通表视图,然后是带有搜索结果对象的表视图:
问题在于尝试使用默认的commiEditingStyle方法删除搜索结果对象。在其他更专业的SO用户的帮助下,我已经包含了两个if子句,一个用于在主题是搜索结果对象时删除对象,第二个用于从正常表视图中删除对象。 到目前为止,这是方法代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
[self.searchResults removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source
NSManagedObjectContext *context = [self managedObjectContext];
ToDoItem *ToDoItemToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
[context deleteObject:ToDoItemToDelete];
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Error: %@",error);
}
}
}
}
从普通表格视图(第一张图像)中删除对象时没有问题。但是尝试删除搜索结果对象(第二张图片)时,应用程序崩溃显示以下异常:
'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0xd17b060'
*** First throw call stack:
(
0 CoreFoundation 0x01aab5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0182e8b6 objc_exception_throw + 44
2 CoreFoundation 0x01b48903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01a9b90b ___forwarding___ + 1019
4 CoreFoundation 0x01a9b4ee _CF_forwarding_prep_0 + 14
5 To-Do Pro Light 0x00004eda -[ToDoItemsTableViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 298
6 UIKit 0x0068cba3 -[UITableView animateDeletionOfRowWithCell:] + 107
7 UIKit 0x0080c695 -[UITableViewCell _swipeDeleteButtonPushed] + 70
8 libobjc.A.dylib 0x01840874 -[NSObject performSelector:withObject:withObject:] + 77
9 UIKit 0x0059e0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
10 UIKit 0x0059e04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
11 UIKit 0x006960c1 -[UIControl sendAction:to:forEvent:] + 66
12 UIKit 0x00696484 -[UIControl _sendActionsForEvents:withEvent:] + 577
13 UIKit 0x00695733 -[UIControl touchesEnded:withEvent:] + 641
14 UIKit 0x00910c7f _UIGestureRecognizerUpdate + 7166
15 UIKit 0x005db19a -[UIWindow _sendGesturesForEvent:] + 1291
16 UIKit 0x005dc0ba -[UIWindow sendEvent:] + 1030
17 UIKit 0x005afe86 -[UIApplication sendEvent:] + 242
18 UIKit 0x0059a18f _UIApplicationHandleEventQueue + 11421
19 CoreFoundation 0x01a3483f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
20 CoreFoundation 0x01a341cb __CFRunLoopDoSources0 + 235
21 CoreFoundation 0x01a5129e __CFRunLoopRun + 910
22 CoreFoundation 0x01a50ac3 CFRunLoopRunSpecific + 467
23 CoreFoundation 0x01a508db CFRunLoopRunInMode + 123
24 GraphicsServices 0x038bc9e2 GSEventRunModal + 192
25 GraphicsServices 0x038bc809 GSEventRun + 104
26 UIKit 0x0059cd3b UIApplicationMain + 1225
27 To-Do Pro Light 0x00007d2d main + 141
28 libdyld.dylib 0x020e7725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
请欢迎任何帮助或建议。
答案 0 :(得分:0)
问题在于你在做什么:
[self.searchResults removeObjectAtIndex:indexPath.row];
我猜测searchResults
是NSArray
,它是不可变的。将其更改为NSMutableArray
(不进行转换,实际更改对象)或更改逻辑/ UX。
例如,允许用户删除搜索结果是否真的有意义?