1
votes

I'm trying to add Row/cell in to tableview, but before [Table endUpdates]; ... app crashes with error :

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2372/UITableView.m:909 * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 33 into section 0, but there are only 33 rows in section 0 after the update'

-(IBAction)createTask
{
    Data  *sharedManager = [Data sharedManager]; //singleton class
    [Table  beginUpdates];

    [Table insertRowsAtIndexPaths:[NSArray arrayWithObject:
                                          [NSIndexPath indexPathForItem: 
                                                [sharedManager._NAME count] inSection:0]] 
                                          withRowAnimation:UITableViewRowAnimationNone];

     // sharedManager._NAME is NSMutableArray wich consists names for cells
    [Task_handler createTask]; // adds objects to NSMutableArray's 
                               // (like names for cels and so on)

    [Table  endUpdates];
    [Table reloadData];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Data  *sharedManager = [Data sharedManager];
    NSLog(@"numberOfRowsInSection : %d",[sharedManager._NAME count]);

    return [sharedManager._NAME count];
}

Thank you in advance.

3
have you ricalculate the number of the row in the delegate method? - Simone Pistecchia
Yes, i do. I have updated my question. - Hariz Hent

3 Answers

1
votes

How about this:

-(IBAction)createTask
{
    Data  *sharedManager = [Data sharedManager]; //singleton class
    [Table  beginUpdates];
    [Table insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForItem: [sharedManager._NAME count]+1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    [Task_handler createTask]; //adds objects to NSMutableArray's (like names for cels and so on)
    [Table reloadData];
    [Table  endUpdates];
}
1
votes

When adding rows to your table, you need to let the TableView know that it has more cells to manage. Before using insertRowsAtIndexPaths:, you need to run the [TaskHandler createTask] method, which will create an object your datasource array. That array should be an instance variable that is counted by your TableView delegate method numberOfRowsInSection:.

-2
votes

Crash was solved by changing code into:

Data  *sharedManager = [Data sharedManager];
[Table  beginUpdates];
[Table insertRowsAtIndexPaths:[NSArray arrayWithObject:
                                          [NSIndexPath indexPathForItem: 
                                                [sharedManager._NAME count] inSection:0]] 
                                      withRowAnimation:UITableViewRowAnimationNone];
[Task_handler createTask];
[Table  endUpdates];