开发者

*** -[CFString rangeOfString:options:]: message sent to deallocated instance 0xbe253f0

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;
                }
            }
       开发者_如何学JAVA }
    }


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜