Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear NSMutableArray for a refresh

I have a couple of NSMutableArrays which i need to clear when refreshing the view. However, when I try to clear them with [array removeAllObjects]; my tableview crashes due to index beyond bounds error. All i do with the refresh, is clear the arrays and call the same function as in viewDidLoad for filling the tableview. [tableView reloadData] doesn't get called until the very last line of the method.

EDIT: It's highly likely that the issue is this: I use a pull to refresh external lib, and when you scroll up and release the table, it bounces back down and thus the UITableView tries to load the next cell, which it cant because the array is cleared, and it's still being loaded.

Answer: removeAllObjects from the arrays, immediately do a self.tableView reloadData and then continue with the rest.

like image 206
MaikelS Avatar asked Oct 19 '11 10:10

MaikelS


2 Answers

problem might be due to numberOfRowsInSection returning some count and your data source array is empty.

just call [array removeAllObjects] and in numberOfRowsInSection return [array count].

I hope it will resolve your issue. Best of Luck!!!

like image 149
Saurabh Passolia Avatar answered Oct 09 '22 19:10

Saurabh Passolia


I delete cells from my table view in the following manner-

NSMutableArray* indexPathsToDelete = [[NSMutableArray alloc] init];
            for(unsigned int i = 0; i < [self.array count]; i++)
            {
                [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:0]];
            }

[self.tableView beginUpdates];
[self.array removeAllObjects];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
[indexPathsToDelete release];
like image 25
Akshay Avatar answered Oct 09 '22 20:10

Akshay