2012-05-20 51 views
0

我遇到了一个问题NSMutableArray给我一个Exc_Bad_Access没有理由。NSMutableArray removeAllObjects问题

我有一个包含大约700个记录UITableView,我想激活滤波处理它,我用以下方法给UITableView的内容过滤:

- (void) filterTableContentWith:(int)_minValue andWith:(int)_maxValue { 

     [tableContent removeAllObjects]; 
     tableContent = [[NSMutableArray alloc] initWithArray:originalTableContent copyItems:YES]; 

     if (_minValue == 0 && _maxValue == 0) { 
      NSLog(@"This is mean that no filter is activate here"); 
     } else { 
      for (int x = ([tableContent count] - 1); x >= 0; x--) { 
       if ([[[tableContent objectAtIndex:x] objectForKey:@"price"] intValue] < _minValue && [[[tableContent objectAtIndex:x] objectForKey:@"price"] intValue] > _maxValue) { 
         [tableContent removeObjectAtIndex:x]; 
       } 
      } 
     } 
     NSLog(@"tableContent count = %@",[tableContent count]); 
     [self.tableView reloadData]; 
} 

当我调用这种方法,它给我Exc_Bad_AccessNSLog(@"tableContent count ...

我认为[tableContent removeAllObjects];是释放数组,但它是不合理的。

任何帮助将不胜感激。

回答

3

count返回int,所以改变你的NSLog到:

NSLog(@"tableContent count = %d",[tableContent count]); 

,你会没事的。

+0

OMG,我多么愚蠢是我,我已经花费约8小时对此,非常感谢... – Scar

0

除了改变你的NSLog用于%d计数,我认为下面的代码是不是你的循环中移除内容更好:

... 
} else { 
    NSPredicate* predicate = [NSPredicate predicateWithFormat: 
    @"price < %d AND price > %d", _minValue, _maxValue]; 
    NSArray* array = [tableContent filteredArrayUsingPredicate:predicate]; 
    tableContent = array; 
} 
... 
+0

谢谢,但'tableContent'是'NSDictionaries'的NSMutableArray',所以,你的建议将不会工作,再次,谢谢。 – Scar