3

有没有人遇到过这个?

当我选择从我的 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通过断点验证,当我选择删除一个单元格时它被击中。我知道保存正在正确处理,因为如果我退出并重新运行应用程序,我选择删除的单元格实际上已被删除。

但是,当我按下删除按钮时,仍然没有任何反应。该按钮保持选中状态,直到我单击其他位置,从而取消选择它。

4

2 回答 2

2

你能展示构建的代码NSFetchedResultsController吗?现在您已经按照@DVG 的建议保存了代码,那么您应该会收到回调。也许您的代表没有正确设置。

更新

正如我怀疑您没有将代表设置在NSFetchedResultsController

[fetchedResultsController setDelegate:self];

在您呼叫的位置添加该行后,您应该开始接收更新-performFetch:

于 2010-08-02T22:02:50.013 回答
1

看起来您需要实现 editorStyleForRowAtIndexPath 以实际将您尝试删除的单元格声明为可删除。

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  return UITableViewCellEditingStyleDelete;
}

此外,您似乎没有保存 managedObjectContext。

编辑:所以听起来你的模型正在提交,但你的 tableview 正在重新加载。

尝试实施

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
  [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
  [self.tableView endUpdates];
}
于 2010-07-30T01:18:16.683 回答