Brad
Brad

Reputation: 85

Removing Objects from UITableView

So I've seen similar topics but I don't quite understand why I am crashing still....

I am reading entries into an array called "entries." After a while I want to add new entries and then delete the old ones.

So I'm essentially adding stuff to 'entries" and then wanting to delete some old entries. I then run the below method and dies out when it starts editing the view. The error is posted below the code block.

Thanks for the help!

-(void) removeOldEntries:  (int) numOfEntries
{

    NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init] ;

 //  Remove the first number of entries in the table view.  This number is specified by the numOfEntries
 for (int i = numOfEntries - 1; i >= 0; i = i -1 )
 {
     [entries removeObjectAtIndex:i];
 }

//  Build deleteIndexPaths
for (int i = numOfEntries - 1; i >= 0; i = i - 1)
{
    //  Add objects to our index pathes array (of things we need to delete) and then remove the objects from feeds array
    [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}

//  Start the editing of the TableView
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
//  End the editing of the table view
[deleteIndexPaths removeAllObjects];
[deleteIndexPaths release]; 
}

Error message:

 *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976
2011-12-27 22:43:04.490 v1.0[999:6003] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (8) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00e3dbe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f925c2 objc_exception_throw + 47
    2   CoreFoundation                      0x00df6628 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x000d847b -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x0035aa0f -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8424
    5   UIKit                               0x0034a433 -[UITableView endUpdates] + 42
    6   v1.0                                0x000050e6 -[NewsTableViewController removeOldArticles:] + 408
    7   v1.0                                0x00004d16 -[NewsTableViewController pullAndParseData] + 696
    8   CoreFoundation                      0x00dae67d __invoking___ + 29
    9   CoreFoundation                      0x00dae551 -[NSInvocation invoke] + 145
    10  Foundation                          0x000ff555 -[NSInvocationOperation main] + 51
    11  Foundation                          0x0006dbd2 -[__NSOperationInternal start] + 747
    12  Foundation                          0x0006d826 ____startOperations_block_invoke_2 + 106
    13  libSystem.B.dylib                   0x96653a24 _dispatch_call_block_and_release + 16
    14  libSystem.B.dylib                   0x96645cf2 _dispatch_worker_thread2 + 228
    15  libSystem.B.dylib                   0x96645781 _pthread_wqthread + 390
    16  libSystem.B.dylib                   0x966455c6 start_wqthread + 30
)
terminate called after throwing an instance of 'NSException'

Upvotes: 0

Views: 1520

Answers (2)

MadhavanRP
MadhavanRP

Reputation: 2830

You are making this needlessly complicated. There is some inconsistency in your deletion which is why there is an error. Make sure in

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return for section 0
    return [entries count];
}

And change your removeOldEntries: to

-(void) removeOldEntries:  (int) numOfEntries
{

[entries removeObjectsInRange:NSMakeRange(0, numOfEntries-1)];
[self.tableView reloadData];
}

Upvotes: 2

tacos_tacos_tacos
tacos_tacos_tacos

Reputation: 10585

Use [_tableView reloadData]. This will simplify your life.

By the way, the exception you are getting is indicating that your array is not containing what you think it should. Trying NSLogging your array.

Upvotes: 2

Related Questions