Q:

cancel button on search bar does not cancel correctly

I have a search bar, i can search now, but when I enter a text to search, and click the cancel button. It does not give me back my first stage, meaning full of the items in the table. 
For example: I search the item with word: a, it gives me all the a items, yes, it is right now, but when i hit the cancel button, i want the programme gives me all the items exist, not just a items.
Here is the code: please help me out. Thank you so much.
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
    searchBar.text = @"";
    [searchBar resignFirstResponder];

    letUserSelectRow = YES;
    searching = NO;
    self.tableView.scrollEnabled = YES;

    NSLog(@"what text after cancel now: %@", searchBar.text);

    [self.tableView reloadData];

}
- (NSMutableArray *) searchTableView {

    NSString *searchText = searchBar.text;
    NSLog(@"search text: %@", searchText);
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    NSMutableArray *tempArr = [[NSMutableArray alloc] init];

    for (NSDictionary *dTemp in arrayData)
    {
        NSString *tempStr = [dTemp objectForKey:@"url"];
        NSLog(@"sTemp string: %@",[ NSString stringWithFormat:@"%@", tempStr]);
        NSRange titleResultsRange = [tempStr rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
        {
            NSLog(@"1 count :%d", [resultArray count]);
            [resultArray addObject:dTemp];
            NSLog(@"2 count :%d", [resultArray count]);
            [tempArr addObject:resultArray];
            [resultArray release];

            resultArray = [NSMutableArray new];
        }

    }
    if (resultArray != nil) {
        [resultArray release];
    }

    return tempArr;
}

- (void)searchBar:(UISearchBar *)aSearchBar textDidChange:(NSString *)searchText
{

    NSLog(@"what text after cancel now: %@", searchBar.text);

    if([searchText length] > 0) {
        [sortedArray removeAllObjects];
        searching = YES;
        letUserSelectRow = YES;
        self.tableView.scrollEnabled = YES;
        NSMutableArray *searchArray = [self searchTableView];
        sortedArray = [[NSMutableArray alloc] initWithArray:searchArray copyItems:YES];

        for (int i = 0; i<[sortedArray count]; i++) {
            NSLog(@"this is the search array: %@", [[sortedArray objectAtIndex:i] class]);
        }

        NSLog(@"sorted array: %d", [sortedArray count]);
    }
    else {

        searching = NO;
        letUserSelectRow = NO;
        self.tableView.scrollEnabled = NO;
    }

    [self.tableView reloadData];
}

A:

You don't need to override any of UISearchBar methods to accomplish this. The new way of doing this relies on the UISearchDisplay controller instead (specifically on shouldReloadTableForSearchString).
Declare your view controller to conform to UISearchDisplayDelegate protocol, and keep two instance variables: your model as NSArray (all data) and a filtered array as NSMutableArray (a subset of your data). The code you presently have in "searchTableView" would filter the content of the model and place it into the filtered NSMutableArray. Then you would override the following UITableView methods: -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. In each, before returning, make a comparison to determine whether your tableView argument is equal to self.searchDisplayController.searchResultsTableView. If it is, the user is looking at the filtered list and your should use the content of the filtered NSMutableArray to create the view, otherwise, the user is looking at the whole data set and you should use the content of the NSArray that holds your model. Take a look at the following Apple code for a simple example of what I described:
http://developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html

