我用搜索栏创建了一个UITableView。首次访问TableView并搜索一切正常。当视图卸载并返回搜索选项并尝试再次搜索时,会出现问题。
- (void)viewDidLoad {
[super viewDidLoad];
self.listContent = [[NSMutableArray alloc] initWithCapacity:20];
self.filteredListContent = [NSMutableArray arrayWithCapacity:[listContent count]];
...
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
/*
Search the main list for buildings whose name match searchText; add items that match to the filtered array.
*/
for (NSArray*rows in self.listContent)
{
for (NSDictionary *row in rows)
{
NSString *locationName = [row objectForKey:@"Name"];
// TODO: BUG - *** -[CFString rangeOfString:options:]: message sent to deallocated instance 0xbe253f0
NSRange titleResultsRange = [locationName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
{
[filteredListContent addObject:row];
}
}
}
}
-(void) locationsReceived:(NSData *)data {
NSString *jsonData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
// Create a dictionary from the JSON string
NSArray *items = [[[jsonData JSONValue] objectForKey:@"ResultSet"] objectForKey:@"Location"];
// Create a dictionary from the JSON string
[listContent release];
listContent = nil;
self.listContent = [[NSMutableArray alloc] initWithCapacity:20];
for (NSDictionary *section in sectionTitles) {
NSMutableArray *rows = [[NSMutableArray alloc] initWithCapacity:20];
[self.listContent addObject:rows];
for (NSDictionary *item in items) {
NSInteger sectionCampus = [[section objectForKey:@"CampusID"] intValue];
NSInteger rowCampus = [[item objectForKey:@"CampusID"] intValue];
if (sectionCampus == rowCampus ) {
//NSDictionary *newDic = [[NSDictionary alloc] initWithDictionary:item copyItems:YES];
//[rows addObject:newDic ];
//[newDic release];
[rows addObject:item ];
} else {
break;
}
}
}
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
/*
Update the filtered array based on the search text and scope.
*/
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
/*
Search the main list for buildings whose name matches searchText; add items that match to the filtered array.
*/
for (NSArray*rows in listContent)
{
for (NSDictionary *row in rows)
{
NSString *locationName = [row objectForKey:@"Name"];
// TODO: BUG - *** -[CFString rangeOfString:options:]: message sent to deallocated instance 0xbe253f0
NSRange titleResultsRange = [locationName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
{
[filteredListContent addObject:row];
}
}
}
}
答案 0 :(得分:0)
我宁愿清空listContent
中的现有-(void) locationsReceived:(NSData *)data
,而不是分配一个新的。{/ p>