问题描述
以前有人遇到过这种情况吗?
Has anyone run into this before?
当我选择从我的tableView中删除一行(由FRC填充)时,应用程序不会崩溃或挂起。它不做任何事情。删除按钮保持选中,如果我单击模拟器上的其他位置,删除按钮取消选择和消失,但单元格从来没有从UI中删除。我确定有一个愚蠢的监督我在这里,但我不能发现它。下面是我的代码的相关部分。
When I choose to delete a row from my tableView (populated by an FRC), the app doesn't crash or hang. It doesn't do anything. The delete button stays selected and if I click elsewhere on the simulator, the delete button deselects and disappears, but the cell is never removed form the UI. I'm sure there is a dumb oversight I am making here, but I can't spot it. Below are the relevant portions of my code.
我有我的UITableViewController接口这样声明:
I have my UITableViewController interface declared as such:
#import <UIKit/UIKit.h> #import "RubricAppDelegate.h" @interface ClassList : UITableViewController <NSFetchedResultsControllerDelegate> { NSMutableArray *classList; NSFetchedResultsController *fetchedResultsController; NSManagedObjectContext *managedObjectContext; } @property(nonatomic,retain) NSMutableArray *classList; @property(nonatomic, retain) NSFetchedResultsController *fetchedResultsController; @property(nonatomic, retain) NSManagedObjectContext *managedObjectContext; - (IBAction) grade:(id)sender; @end
在其实现文件中,我有:
In its implementation file, I have:
- (void)viewDidLoad { [super viewDidLoad]; RubricAppDelegate *appDelegate = (RubricAppDelegate *)[[UIApplication sharedApplication] delegate]; managedObjectContext = [appDelegate managedObjectContext]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"myClass" inManagedObjectContext:managedObjectContext]; NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; [request setEntity:entity]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"classID" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; [request setSortDescriptors:sortDescriptors]; [sortDescriptor release]; fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; NSError *error; [fetchedResultsController performFetch:&error]; }
和
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { myClass *result = (myClass *)[fetchedResultsController objectAtIndexPath:indexPath]; [managedObjectContext deleteObject:result]; NSError *error; [managedObjectContext save:&error]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { //Not yet implemented } }
并且
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; switch(type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } }
$ b
Also
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == [[fetchedResultsController fetchedObjects] count]) { return UITableViewCellEditingStyleInsert; } return UITableViewCellEditingStyleDelete; }
上述代码使单元格超出了我获取填充的单元格,但其余的删除单元格。
That above code makes the cell just beyond my fetch-populated cells an insert cell, but the rest delete cells.
我有我的UITableView连接到我的文件的所有者为其委托和数据源在IB。我需要更改以获取 NSFetchedResultsChangeDelete:才能触发?
I have my UITableView connected to my File's Owner for its delegate and datasource in IB. What do I need to change to get NSFetchedResultsChangeDelete: to trigger?
我已将 [managedObjectContext save:& error]; 添加到 commitEditingStyle 通过断点,当我选择删除单元格时,它被点击。我知道保存正在处理正确,因为如果我退出并重新运行应用程序,我选择删除的单元格事实上被删除。
I have added [managedObjectContext save:&error]; to commitEditingStyle and verified via breakpoint that it is being hit when I choose to delete a cell. I know the save is processing correctly, because if I quit and re-run the app, the cells that I chose to delete are in fact deleted.
但是,当我按下删除按钮,仍然没有发生。
However, when I press the delete button, there is still nothing happening. The button stays selected until I click elsewhere, thereby deselecting it.
推荐答案
您可以显示构建<$ c $的代码c> NSFetchedResultsController ?现在你有你的代码保存为@DVG建议,那么你应该得到回调。
Can you show the code that builds the NSFetchedResultsController? Now that you have the save in your code as @DVG suggested then you should be getting callbacks. Perhaps your delegate is not being set properly.
因为我怀疑你没有设置委托 NSFetchedResultsController :
As I suspected you are not setting the delegate on the NSFetchedResultsController:
[fetchedResultsController setDelegate:self];
您应该在调用 -performFetch :。
这篇关于NSFetchedResultsChangeDelete未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!