tags:

views:

50

answers:

1

I've created a UITableView with a search bar. When first accessing the TableView and searching everything works fine. The problem arises when the view unloads and you go back to the search option and try to search again.

    - (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];
            }
        }
    }
}
A: 

I would rather empty the existing listContent in -(void) locationsReceived:(NSData *)data instead of allocating a new one.

tob
I changed the code to [self.listContent removeAllObjects] in -(void) locationsReceived:(NSData *)data, and I still get the same error.
I can only guess: Maybe `[jsonData JSONValue]` does not retain the value it returns? Maybe your other code is doing something weird which is not included in your excerpt.
tob
I think the problem is when I filter the search results into the filteredListContent list, I am adding a reference to dictionary object from the listContent array to the filteredListContent array. But the problem I think might be that I am calling [self.filteredListContent removeAllObjects], which might be deleting the dictionary object from the listContent array. I am going to try to deep copy dictionary object and see if that solves the problem.