Hello all,
I've started a project and I have an indexed UITableView displaying a list of data in sections (alphabet), I also have 3 buttons in the view and touching them allows the user to filter the data.
I have one button called "Browse all", another one called "Active" and the last one called "Inactive".
What I've put together is having an original array of data. And when I want to filter that, I copy that array into a temporary array and loop through each letter (section), and each entry per section (array of my data objects).
If according to the filter I should not be displaying a particular object in the list, I use NSMutableArray's removeObjectAtIndex method to remove that object.
All works fine but when I select Browse all after filtering the data previously, I realise that in fact when I used removeObjectAtIndex I actually made some objects release from memory.
Could anyone point out why in fact the items of my NSMutableDictionary reference the original objects and not a copy of these items ?
Code:
filteredArray = nil;
NSMutableArray* tmpArray = [fullArray mutableCopy];
if(filterActive){
for (int u = 0; u < [tmpArray count]; u++) {
NSMutableDictionary* d = [tmpArray objectAtIndex:u];
NSMutableArray* dataObjects = [d objectForKey:@"Rows"];
for(int i = 0; i < [dataObjects count]; i++){
myObjectType* f = [dataObjects objectAtIndex:i];
if(f.badge == 1){
[dataObjects removeObjectAtIndex:i];
i--;
}
}
if([dataObjects count] == 0){
[tmpArray removeObjectAtIndex:u];
u--;
}
}
}
filteredArray = [[NSArray alloc] initWithArray:tmpArray];
// I use filteredArray as the data source for the UITableView