Iphone UITableView处于编辑模式-按Delete键使我的应用程序崩溃

Iphone UITableView处于编辑模式-按Delete键使我的应用程序崩溃,iphone,objective-c,cocoa-touch,uitableview,core-data,Iphone,Objective C,Cocoa Touch,Uitableview,Core Data,我的UITableView有一个编辑按钮,可添加一个“插入行”(带绿色加号)。如果在编辑模式下尝试删除行,我的应用程序将崩溃并显示“NSRangeException”。在不处于编辑模式(使用滑动删除)时删除行是可以的 我知道这与表视图认为它有多少行有关-获得一个插入行和滑动删除两行以在同一个表中工作是一个噩梦(对于像我这样的初学者来说),因为滑动和按下编辑按钮都会使表处于“编辑模式”,但编辑按钮会添加一行,而滑动不会 我一直在尝试使用一些IVAR来克服这个问题,但很明显我在某个地方出了问题。有经

我的UITableView有一个编辑按钮,可添加一个“插入行”(带绿色加号)。如果在编辑模式下尝试删除行,我的应用程序将崩溃并显示“NSRangeException”。在不处于编辑模式(使用滑动删除)时删除行是可以的

我知道这与表视图认为它有多少行有关-获得一个插入行和滑动删除两行以在同一个表中工作是一个噩梦(对于像我这样的初学者来说),因为滑动和按下编辑按钮都会使表处于“编辑模式”,但编辑按钮会添加一行,而滑动不会

我一直在尝试使用一些IVAR来克服这个问题,但很明显我在某个地方出了问题。有经验的人能告诉我哪里出了问题吗?(我敢肯定,这件事太复杂了!)

编辑-根据Izzy的要求,我把我的整个未编辑的.m文件放在这里

//
//  ChecklistsViewController.m
//  Check Box
//
//  Created by Ric on 18/03/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ChecklistsViewController.h"
#import "Checklist.h"

@interface ChecklistsViewController (private)
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
- (void)addingView;
@end


@implementation ChecklistsViewController

@synthesize category, managedObjectContext, fetchedResultsController;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        editingFromSwipe = NO;
        tableIsEditing = NO;
        NSLog(@"tableIsEditing = NO (init) \n");
        NSLog(@"Not Editing From Swipe (init) \n");
    }
    return self;
}

- (void)dealloc
{
    [category release];
    [managedObjectContext release];
    [fetchedResultsController release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    editingFromSwipe = NO;
    tableIsEditing = NO;
    NSLog(@"tableIsEditing = NO (viewDidLoad) \n");
    NSLog(@"Not Editing From Swipe (viewDidLoad) \n");

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    self.navigationItem.rightBarButtonItem = self.editButtonItem;    
    self.tableView.allowsSelectionDuringEditing = YES;
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection HAS BEEN CALLED");
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    int rows = [sectionInfo numberOfObjects];

    if (self.editing) {
        if (!editingFromSwipe && tableIsEditing) {
            NSLog(@"Returning extra row - editing not from swipe \n");
            return rows +1;
        }
        NSLog(@"Returning normal rows - editing from swipe \n");
        return rows;
    }
    NSLog(@"Returning normal rows - not editing - and setting tableIsEditing back to NO \n"); 
    tableIsEditing = NO;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    NSLog(@"Should go into if statement here! \n");

    if (tableView.editing) { //
        if ((indexPath.row == 0) && (!editingFromSwipe)) {
            NSLog(@"Configuring Add Button Cell while editing \n");
            cell.textLabel.text = @"Add New Checklist";
            cell.detailTextLabel.text = nil;
        }
        else {
            NSLog(@"Configuring other cells while editing \n");
            [self configureCell:cell atIndexPath:indexPath];
        }

    }
    else {
        NSLog(@"Configuring Cell Normally While Not Editing \n");
        [self configureCell:cell atIndexPath:indexPath];
    }


    return cell;
}


// Override to support conditional editing of the table view.
/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // return (indexPath.row != 0);
    return YES;
}
*/


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the managed object for the given index path
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];        
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

        // Save the context.
        NSError *error = nil;
        if (![context save:&error])
        {
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }    
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        [self addingView];

    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    int row = indexPath.row;

    if (self.editing && row == 0) {
        if (!editingFromSwipe && tableIsEditing) {
            NSLog(@"Not from swipe so making first row style insert button \n");
            return UITableViewCellEditingStyleInsert;
        }
        else if (editingFromSwipe) { 
            NSLog(@"Is from swipe so making first row delete rather than insert \n");
            return UITableViewCellEditingStyleDelete;
        }

    }
    NSLog(@"Not editing or not first row so making cell style normal \n");
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingFromSwipe = YES;
    // NSLog(@"Editing From Swipe (just swiped) \n");
    [super tableView:tableView willBeginEditingRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView didEndEditingRowAtIndexPath:indexPath];
    editingFromSwipe = NO;
    // NSLog(@"Not Editing From Swipe (just unswiped) \n");
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    NSArray *addRow = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil];
    [self.tableView beginUpdates];

    if (!editingFromSwipe) {
        if (editing) {
            NSLog(@"Editing called while swipe off, so adding insert row \n");
            tableIsEditing = YES;
            NSLog(@"tableIsEditing = YES (setEditing:Animated:)");
            [self.tableView insertRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft];
        }
        else {
            [self.tableView deleteRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft];
        } 
    }
    [self.tableView endUpdates];
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != 0) {
        // Navigation logic may go here. Create and push another view controller.
        /*
         <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
         [self.navigationController pushViewController:detailViewController animated:YES];
         [detailViewController release];
         */
    }
}


#pragma mark - Data


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    Checklist *aChecklist = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = aChecklist.name; // description];
    cell.detailTextLabel.text = aChecklist.category.name; // description];
}


- (void) addingView// :(id)sender
{
    // Turn off edit mode if it is on.
    //if (self.editing) {
    //    [self.navigationController setEditing:NO animated:YES];
    //}

    //Create the root view controller for the navigation controller
    AddingViewController *viewController = [[AddingViewController alloc] initWithNibName:@"AddingViewController" bundle:nil];

    viewController.delegate = self;
    viewController.title = @"Add Checklist";

    // Create the navigation controller and present it modally
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [self presentModalViewController:navigationController animated:YES];

    viewController.textLabel.text = @"Enter new checklist name";

    [navigationController release];
    [viewController release];
}


#pragma mark - AddingViewDelegate


- (void)addingViewController:(AddingViewController *)addingViewController didAdd:(NSString *)itemAdded
{
    if (itemAdded != nil) {

        // Turn off editing mode.
        if (self.editing) [self.navigationController setEditing:NO animated:NO];

        // Add the category name to our model and table view.

        // Create a new instance of the entity managed by the fetched results controller.
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        Checklist *newChecklist = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

        [category addChecklistsObject:newChecklist];

        newChecklist.name = itemAdded;        
        // [newChecklist setDateStamp:[NSDate date]];

        // Save the context.
        NSError *error = nil;
        if (![context save:&error])
        {
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }


    }

    [self dismissModalViewControllerAnimated:YES];
}


#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController
{
    if (fetchedResultsController != nil)
    {
        return fetchedResultsController;
    }

    // Set up the fetched results controller.

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Checklist" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Set 4* the predicate so we only see checklists for this category.
    NSPredicate *requestPredicate = [NSPredicate predicateWithFormat:@"category.name = %@", self.category.name];
    [fetchRequest setPredicate:requestPredicate];    
    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];    
    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];    
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".    

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                                                                managedObjectContext:self.managedObjectContext 
                                                                                                  sectionNameKeyPath:nil 
                                                                                                           cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;


    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
    {
       // handle the error properly!
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return fetchedResultsController;
} 


#pragma mark - Fetched results controller delegate


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


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type)
    {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (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;
    }
}


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


/*
 // Implementing the above methods to update the table view in response to individual changes may have performance implications if a large number of changes are made simultaneously. If this proves to be an issue, you can instead just implement controllerDidChangeContent: which notifies the delegate that all section and object changes have been processed. 

 - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
 {
 // In the simplest, most efficient, case, reload the table view.
 [self.tableView reloadData];
 }
 */




@end

应用程序不再崩溃,滑动删除仍然可以,但是如果在编辑模式下尝试删除最后一行,它会删除最后一行。其他几排都可以。(顺便说一句,我的插入行位于顶部)

这就是我们所知道的:您在这一行上得到
NSRangeException

[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
deleteObject:
不会抛出
NSRangeException
,因此我们知道

[self.fetchedResultsController objectAtIndexPath:indexPath]
是什么导致了这次撞车。最可能的原因是,在添加“插入行”时,没有考虑到表视图的索引路径与
fetchedResultsController
的索引路径不同

如果我是对的,它只会在删除最后一行时崩溃

这是您修复它的方式:

[self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]]
请记住,只有在“插入行”可见时才应更改索引路径

编辑:

删除插入行下方的行可以正常工作。我可以通过滑动删除任何行,使用编辑按钮可以删除除最后一行之外的任何行(这将删除其上方的行)

这就是导致这个错误的原因:

numberOfRows == rowBeingDeleted
只有当索引路径是最后一行时,才能更改它。这让人困惑,但也许你不应该改变索引路径?尝试将
numberOfRows==rowBeingDeleted
替换为
NO
,看看会发生什么

编辑2:如果改为这样做会发生什么

if (tableIsEditing && !editingFromSwipe) {
    [context deleteObject:[self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]]];
} else {
    [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
}

里克,你能把整个.m文件都贴出来吗?您的tableView:CommittedItingStyle:forRowAtIndexPath:看起来不错。注意:不要在代码中使用abort()。@Izzy-我已经用我的整个.m文件替换了上面的第一个代码段。任何帮助都将不胜感激!abort()只是苹果新CoreData文件样板文件的一部分-我还没有机会替换(嗯,了解)错误代码。再次感谢Erik-我学到了很多!:-)我现在不在电脑旁,但我明天会试试这个。我在这里的时候,希望你不要介意我再次向你提出一些建议。我使用了这两个IVAR(“editingFromSwipe”和“tableIsEditing”)来跟踪我的表中有多少行,这似乎是可行的,但我觉得我把它复杂化了(有两个IVAR的bool值到处被重置)。有没有一种更优雅的方式,我可以让我的插入行和滑动删除玩得很好?我确信只有一个ivar就可以了。@Erik B,我已经尝试合并了该代码(每当同时按下编辑按钮和用户试图删除最后一行时调用它),它已经修复了崩溃,但现在最后一行的第二行被删除了!我已经编辑了我的问题并添加了新代码。@Ric Levy,如果删除插入行正下方的行会发生什么?@Erik B,删除插入行正下方的行可以。我可以通过滑动删除任何一行,使用编辑按钮可以删除除最后一行之外的任何一行(这会删除上面的行)。@Ric Levy,这听起来很奇怪,但如果它按照您描述的方式工作,有一种简单的方法可以修复它。我会更新我的答案给你看。
numberOfRows == rowBeingDeleted
if (tableIsEditing && !editingFromSwipe) {
    [context deleteObject:[self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]]];
} else {
    [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
}