我为NSMutableArray
创建了一个属性,当为其赋值时,我写了以下代码
[self setTotalCells:[[NSMutableArray alloc] initWithArray:[jsonDictionary valueForKey:@"data"]]];
我在搜索栏方法中使用它。但是,一旦我使用了该属性,那么数组就会变空。
cells = [[NSMutableArray alloc]init];
NSLog(@"filterContentForSearchText");
NSLog(@"**************************************************************");
if ([totalCells count]<= 0) {
totalCells = [_jsonPropertiesDictionary valueForKey:@"data"];
}
NSLog(@"%@",totalCells);
NSLog(@"**************************************************************");
for (id cell in totalCells)
{
NSLog(@"bedrooms : %@",[cell valueForKey:@"BEDROOMS"]);
NSLog(@"DISPLAY_ADDRESS : %@",[cell valueForKey:@"DISPLAY_ADDRESS"]);
NSLog(@"PRICE : %@",[cell valueForKey:@"PRICE"]);
NSLog(@"searchText : %@",searchText);
NSComparisonResult result = NSOrderedAscending;
if ([[cell valueForKey:@"BEDROOMS"] isKindOfClass:[NSString class]] && [[cell valueForKey:@"BEDROOMS"] length] > 0) {
result = [[cell valueForKey:@"BEDROOMS"] compare:searchText options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
[self.filteredListContent addObject:cell];
}
if ([[cell valueForKey:@"DISPLAY_ADDRESS"] isKindOfClass:[NSString class]] && [cell valueForKey:@"DISPLAY_ADDRESS"] != nil && [cell valueForKey:@"DISPLAY_ADDRESS"] != @"" && [[cell valueForKey:@"DISPLAY_ADDRESS"] length] > 0) {
result = [[cell valueForKey:@"DISPLAY_ADDRESS"] compare:searchText options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
[self.filteredListContent addObject:cell];
}
if ([[cell valueForKey:@"PRICE"] isKindOfClass:[NSString class]] && [cell valueForKey:@"PRICE"] != nil && [[cell valueForKey:@"PRICE"] length] > 0) {
result = [[cell valueForKey:@"PRICE"] compare:searchText options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
[self.filteredListContent addObject:cell];
}
if ([searchText isEqualToString:@"" ]) {
filteredListContent = totalCells;
break;
}
}
if (self.filteredListContent.count > 0) {
cells = self.filteredListContent;//added by prajakta
}
[self.propertyList_table reloadData];
NSLog(@"filteredListContent : %@ ",filteredListContent);
我已将此属性声明为
@property(atomic,retain)NSMutableArray *totalCells;
任何帮助将不胜感激
答案 0 :(得分:0)
为什么不用这种方式过滤?它不会毁了你的原始数据源......
cells = [[NSMutableArray alloc]init];
NSLog(@"filterContentForSearchText");
NSLog(@"**************************************************************");
if ([totalCells count]<= 0) {
totalCells = [_jsonPropertiesDictionary valueForKey:@"data"];
}
NSLog(@"%@",totalCells);
NSLog(@"**************************************************************");
/*
* replace your code FROM here...
*/
NSArray *filteredCells = [totalCells filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSDictionary *cell = (NSDictionary *)evaluatedObject;
if ([[cell valueForKey:@"BEDROOMS"] isKindOfClass:[NSString class]] && [[cell valueForKey:@"BEDROOMS"] length] > 0) {
return [[cell valueForKey:@"BEDROOMS"] compare:searchText options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
}
if ([[cell valueForKey:@"DISPLAY_ADDRESS"] isKindOfClass:[NSString class]] && [cell valueForKey:@"DISPLAY_ADDRESS"] != nil && [cell valueForKey:@"DISPLAY_ADDRESS"] != @"" && [[cell valueForKey:@"DISPLAY_ADDRESS"] length] > 0) {
return [[cell valueForKey:@"DISPLAY_ADDRESS"] compare:searchText options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
}
if ([[cell valueForKey:@"PRICE"] isKindOfClass:[NSString class]] && [cell valueForKey:@"PRICE"] != nil && [[cell valueForKey:@"PRICE"] length] > 0) {
return [[cell valueForKey:@"PRICE"] compare:searchText options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
}
return FALSE;
}]];
/*
* TO here...
*/
if (self.filteredListContent.count > 0) {
cells = self.filteredListContent;//added by prajakta
}
[self.propertyList_table reloadData];
NSLog(@"filteredListContent : %@ ",filteredListContent);