0

我有一个数组,它在页面加载时包含 200 个对象,我将这些(可变副本)复制到一个名为 filtersarray 的临时数组中,然后将其显示在 uitableview 中。这一切都很好:)

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 :)

然后我尝试重新加载我的表格视图(再次使用过滤数组),当我单步执行时,过滤数组中的前几个对象似乎工作正常,但是如果这是正确的词,过滤数组似乎被“清空”,并且我的代码在之后崩溃表已开始刷新。我确定这一定是内存问题或其他问题。我对目标 c 很陌生,所以任何帮助都将不胜感激。我在下面列出了我的一些代码

- (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"

并且我的过滤数组在 my.h 文件中声明为 nsmutable 数组

NSMutableArray *filteredArray;
4

2 回答 2

1

由于您有一个 NSMutableArray,我建议您使用filterUsingPredicate:而不是filteredArrayUsingPredicate:. 您开始使用的过滤数组被保留,因为您通过副本获得它,但您在change_segment:方法中替换它的不是。

(我怀疑这是问题所在,但崩溃的详细信息及其相关异常会使诊断更容易。)

于 2012-05-12T19:41:52.573 回答
0

保留你的数组。在 -dealloc 中释放它。

filtersArray = [appDelegate.originalArray mutableCopy]retain];

于 2012-05-12T20:46:03.440 回答