Problem with deleting row in sections

hi all, have a problem with deleting row, im doing that what i saw in tutorial, add canEditRow: and commitEditing: , but dont work it, i do not understood what to do. have a issue

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UITableView.m:1700 2016-09-19 20:06:01.562 Application[5360:539117] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (5) must be equal to the number of sections contained in the table view before the update (6), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

#import "MainTableViewController.h"
#import "CustomTableViewCell.h"
#import "CarObject.h"
#import "EditTableViewController.h"

@interface MainTableViewController () <UISearchResultsUpdating>

@property (strong, nonatomic) NSMutableArray *carSection;
@property (strong, nonatomic) NSMutableArray *carRow;

@property (strong, nonatomic) UISearchController *searchController;
@property (strong, nonatomic) NSMutableArray *filteredCars;

@property (strong, nonatomic) NSMutableArray *sectionTitles;
@property (strong, nonatomic) NSMutableDictionary *indicesDictionary;

@end

@implementation MainTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupCarForIndexing];
    [self createSearchController];

    _filteredCars = [NSMutableArray new];

    CGPoint contentOffset = self.tableView.contentOffset;
    contentOffset.y += _searchController.searchBar.frame.size.height;
    self.tableView.contentOffset = contentOffset;

    [self.tableView reloadData];
}

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

    [self saveDataJSON];

    [self.tableView reloadData];
}

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

    [self setupCarForIndexing];

    [self.tableView reloadData];
}

- (void)createSearchController {
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];;
    _searchController.searchResultsUpdater = self;
    _searchController.dimsBackgroundDuringPresentation = false;
    self.definesPresentationContext = true;

    self.tableView.tableHeaderView = _searchController.searchBar;
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = _searchController.searchBar.text;
    [_filteredCars removeAllObjects];

    for (NSMutableArray *section in _carSection) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.mark contains[c] %@", searchString];
        [_filteredCars addObjectsFromArray:[section filteredArrayUsingPredicate:predicate]];
    }

    [self.tableView reloadData];
}

- (void)setupCarForIndexing {
    NSInteger sectionTitleCount = [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
    NSMutableArray *allSections = [[NSMutableArray alloc] initWithCapacity:sectionTitleCount];
    for (NSInteger a = 0; a < sectionTitleCount; a++) {
        [allSections addObject:[NSMutableArray new]];
    }

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *documentDirectory = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    NSURL *saveFile = [documentDirectory URLByAppendingPathComponent:@"car.json"];

    NSError *error;
    NSData *jsonData;
    NSString *json = [[NSString alloc] initWithContentsOfURL:saveFile encoding:NSUTF8StringEncoding error:&error];

    if (json) {
        jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"new");
    } else {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"car" ofType:@"json"];
        NSURL *jsonFile = [NSURL fileURLWithPath:path];
        jsonData = [NSData dataWithContentsOfURL:jsonFile];
        NSLog(@"default");
    }

    _carRow = [NSMutableArray new];
    NSArray *entries = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary * entry in entries) {
        CarObject *car = [CarObject new];
        car.mark = entry[@"mark"];
        car.model = entry[@"model"];
        car.year = [entry[@"year"] integerValue];
        NSData *data = [[NSData alloc] initWithBase64EncodedString:entry[@"image"] options:NSDataBase64DecodingIgnoreUnknownCharacters];
        car.image = [UIImage imageWithData: data];

        NSInteger sectionNumber = [[UILocalizedIndexedCollation currentCollation] sectionForObject:car collationStringSelector:@selector(mark)];
        [allSections[sectionNumber] addObject:car];

        [_carRow addObject:car];
    }

    _sectionTitles = [NSMutableArray new];
    _indicesDictionary = [NSMutableDictionary new];
    _carSection = [NSMutableArray new];
    for (NSInteger b = 0; b < sectionTitleCount; b++) {
        NSArray *currentCars = allSections[b];
        NSString *sectionTitle = [[UILocalizedIndexedCollation currentCollation] sectionTitles][b];
        if (currentCars.count > 0) {
            [_sectionTitles addObject:sectionTitle];
            [_carSection addObject:[currentCars sortedArrayUsingSelector:@selector(compare:)]];
        }
        _indicesDictionary[sectionTitle] = [NSNumber numberWithInteger:MAX((int)_carSection.count - 1, 0)];
    }

}

- (void)saveDataJSON {
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSURL * documentDirectory = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:false error:nil];
    NSURL * saveFile = [documentDirectory URLByAppendingPathComponent:@"car.json"];

    NSMutableArray *new = [NSMutableArray new];
    for (CarObject *car in _carRow) {
        NSMutableDictionary *objectContainer = [NSMutableDictionary new];
        objectContainer[@"mark"] = car.mark;
        objectContainer[@"model"] = car.model;
        objectContainer[@"year"] = @(car.year);
        objectContainer[@"image"] = [UIImageJPEGRepresentation(car.image, 1.0) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

        [new addObject:objectContainer];
    }

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:new options:NSJSONWritingPrettyPrinted error:nil];
    [jsonData writeToURL:saveFile atomically:true];

    NSLog(@"save");
}

- (IBAction)addButtonAction:(id)sender {


}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    if (_searchController.active) {
        return nil;
    } else {
        NSMutableArray *retval = [NSMutableArray arrayWithObject:UITableViewIndexSearch];
        [retval addObjectsFromArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
        return retval;
    }
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    if (index == 0) {
        CGRect searchBarFrame = _searchController.searchBar.frame;
        [tableView scrollRectToVisible:searchBarFrame animated:true];
        return NSNotFound;
    } else {
        return [_indicesDictionary[title] integerValue];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_searchController.active) {
        return 1;
    } else {
        return _carSection.count;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_searchController.active) {
        return _filteredCars.count;
    } else {
        NSMutableArray *carSection = _carSection[section];
        return carSection.count;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (_searchController.active) {
        return nil;
    } else {
        return _sectionTitles[section];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"carCell" forIndexPath:indexPath];
    if (_searchController.active) {
        CustomTableViewCell *carCell = (CustomTableViewCell *)cell;
        CarObject *car = _filteredCars[indexPath.row];
        carCell.carImage.image = car.image;
        carCell.carMarkLabel.text = car.mark;
        carCell.carModelLabel.text = car.model;
        carCell.carYearLabel.text = [NSString stringWithFormat:@"%li", (long)car.year];

        return carCell;
    } else {
        CustomTableViewCell *carCell = (CustomTableViewCell *)cell;
        NSMutableArray *carSection = _carSection[indexPath.section];
        CarObject *car = carSection[indexPath.row];
        carCell.carImage.image = car.image;
        carCell.carMarkLabel.text = car.mark;
        carCell.carModelLabel.text = car.model;
        carCell.carYearLabel.text = [NSString stringWithFormat:@"%li", (long)car.year];

        return carCell;
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return true;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [_carSection removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"goToEdit"]) {
        EditTableViewController *vc = segue.destinationViewController;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

        if (_searchController.active) {
            CarObject *car = _filteredCars[indexPath.row];
            vc.car = car;
        } else {
            NSMutableArray *section = _carSection[indexPath.section];
            CarObject *car = section[indexPath.row];
            vc.car = car;
        }
    }
}

Take a look at your commitEditingStyle method. The call to removeObjectAtIndex is deleting an entire section instead of a single car within a section.

Instead of
[_carSection removeObjectAtIndex:indexPath.row];
you probably want
[_carSection[indexPath.section] removeObjectAtIndex:indexPath.row];

yes, i tried that method but have a new issue

2016-09-20 17:45:33.480 Application[7695:821782] -[CarObject removeObjectAtIndex:]: unrecognized selector sent to instance 0x7a9e7d40
2016-09-20 17:45:37.228 Application[7695:821782] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CarObject removeObjectAtIndex:]: unrecognized selector sent to instance 0x7a9e7d40'

Hmm. Worked for me. :frowning:
Can you post your updated commitEditingStyle method?

sure

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (_searchController.active) {
            [_filteredCars removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        } else {
            [_carSection[indexPath.section] removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
            
            [tableView reloadData];
        }
    }
}