誰かがこれに遭遇したことがありますか?
tableView(FRCによって入力された)から行を削除することを選択した場合、アプリはクラッシュまたはハングしません。何もしません。削除ボタンは選択されたままで、シミュレーターの他の場所をクリックすると、削除ボタンの選択が解除されて消えますが、セルがUIから削除されることはありません。私がここで行っているばかげた見落としがあると確信していますが、私はそれを見つけることができません。以下は私のコードの関連部分です。
UITableViewControllerインターフェイスを次のように宣言しています。
#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
その実装ファイルには、次のものがあります。
- (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;
}
}
また
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [[fetchedResultsController fetchedObjects] count]) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
上記のコードは、フェッチで入力されたセルのすぐ先のセルを挿入セルにしますが、残りはセルを削除します。
UITableViewを、IBのデリゲートとデータソースのファイルの所有者に接続しています。NSFetchedResultsChangeDelete:
トリガーするには何を変更する必要がありますか?
アップデート:
セルを削除することを選択したときにヒットしていることをブレークポイントに追加[managedObjectContext save:&error];
し、ブレークポイントを介して確認しました。commitEditingStyle
アプリを終了して再実行すると、削除することを選択したセルが実際に削除されるため、保存が正しく処理されていることがわかります。
しかし、削除ボタンを押しても何も起こりません。ボタンは、他の場所をクリックするまで選択されたままになり、選択を解除します。