I have an array which has 200 objects in it on page load i copy these (mutable copy) to a temp array called filteredarray which i then display in a uitableview. This all works fine 🙂
i then have a segment selector which when selected is supposed to filter my orginal array using a predictate and filteredarray will now hole the contents of the objects which meet the predictate criteria again from what i can see this is working fine 🙂
i then try and reload my table view ( again with filteredarray ) which when i step through seems to work ok for the first couple objects in filteredarray but then filteredarray seems to be “emptied” if thats the right word, and my code crashes just after the table has started to be refreshed. Im sure it must be a memory issue or something. im pretty new to objective c so any help would be much appreciated. i have listed some of my code below
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
filteredArray = [appDelegate.originalArray mutableCopy];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellidentifier = @"customcell";
customcell *cell = (customcell *)[tableView dequeueReusableCellWithIdentifier:
cellidentifier];
array_details *arrayObj = [filteredArray objectAtIndex:indexPath.row];
// UITableViewCell cell needs creating for this UITableView row.
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[customcell class]]) {
cell = (customcell *) currentObject;
break;
}
}
}
cell.line1.text = arrayObj.line1;
cell.line2.text = arrayObj.line2;
return cell;
}
-(IBAction)change_segment:(id)sender;{
if(segmentcontrol.selectedSegmentIndex == 2){
filteredArray = [appDelegate.originalArray mutableCopy];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"selected == YES"];
filteredArray = [filteredArray filteredArrayUsingPredicate:testForTrue];
}
[my_table reloadData]; // its when i do this that the array filtered array seems to somehow get "emptied"
and my filteredarray is declared in my.h file as a nsmutable array
NSMutableArray *filteredArray;
Since you have a NSMutableArray, I would suggest using
filterUsingPredicate:instead offilteredArrayUsingPredicate:. The filtered array you start with is retained because you obtain it via a copy but the one you replace it with in yourchange_segment:method isn’t.(I suspect this is the problem but details of the crash and its related exception would make diagnosis easier.)