我有一个数组,它在页面加载时包含 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;