NSRangeException |从表中删除行(视图)

时间:2014-03-07 13:49:19

标签: ios uitableview nsfetchedresultscontroller

从我的TableView中删除一些行时出现以下错误

    *** Terminating app due to uncaught exception 'NSRangeException', reason: 
'-[__NSCFArray removeObjectAtIndex:]: index (0) beyond bounds (0)'

在这里发现了类似的问题: Crash when moving a row in UITableView

但答案是:

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

似乎无法使用最新的代码 - 我在“NSFetchedResultsController”中收到“预期的类型”错误

任何提示?

欢呼声

编辑:似乎与滑动删除中移动的行有关 - 希望有帮助..

编辑二:有时我会得到一个不同的错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'

罪魁祸首只能在以下某处:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    favsTwo = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"];
    [self.tableView reloadData];
    NSLog(@"Number of items in my array is: %d", [favsTwo count]);
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Number of items in my array is: %d", indexPath.row+1);
    [favsTwo removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [[NSUserDefaults standardUserDefaults] setObject:favsTwo forKey:@"Favorites"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [favsTwo removeObjectAtIndex:indexPath.row];
    [tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.leftBarButtonItem = _sidebarButton;
    _sidebarButton.target = self.revealViewController;
    _sidebarButton.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

}

1 个答案:

答案 0 :(得分:0)

  

favsTwo = [[NSUserDefaults standardUserDefaults] objectForKey:@“Favorites”];

此行为您提供NSArray的实例。如果您期望/想要一个可变数组,那么您需要使用mutableCopy

favsTwo = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"] mutableCopy];

此外,在tableView:commitEditingStyle:forRowAtIndexPath:中,您要拨打[favsTwo removeObjectAtIndex:indexPath.row];两次,这不太可能是您想要做的。删除第二个电话。您还应该移除对reloadData的来电,因为您正在呼叫deleteRowsAtIndexPaths:...