views:

25

answers:

0

Hello fellows, I have the following problem: my app has a table view with subtitle style and a search bar in one tab and if I search something the subtitile is in the wrong spot (ex. I have Object1, Object2, Object3 with ObjectSubtitle 1, ObjectSubtitle and ObjectSubtitle3. If I search for Object2 it's subtitle will show ObjectSubtitle1).

Here's my code… where am I missing something?

//===== Search Bar ==============================================================================================================
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
    if(searching)
        return;


    searching = NO;
    letUserSelectRow = YES;
}

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

    //Remove all objects first.
    [copyListOfItems removeAllObjects];
    [copyListOfItemsSubtitle removeAllObjects];

    if([searchText length] > 0) {

        searching = YES;
        letUserSelectRow = YES;
        self.tableView.scrollEnabled = YES;
        [self searchTableView];
    }

    else {

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

    [self.tableView reloadData];
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {

    [self searchTableView];
}

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    for (NSDictionary *dictionary in listOfItems)
    {
        NSArray *array = [dictionary objectForKey:@"Items"];
        [searchArray addObjectsFromArray:array];
    }
    for (NSDictionary *dictionary in listOfItemsSubtitle)
    {
        NSArray *array = [dictionary objectForKey:@"ItemsDetail"];
        [searchArray addObjectsFromArray:array];
    }

    for (NSString *sTemp in searchArray)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:sTemp];
            [copyListOfItemsSubtitle addObject:sTemp];
    }

    [searchArray release];
    searchArray = nil;
}

//===== Table View ==============================================================================================================
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (searching)
        return 1;
    else
        return[listOfItems count];
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (searching)
        return [copyListOfItems count];
    else {

        //Number of rows it should expect should be based on the section
        NSDictionary *dictionary =[listOfItems objectAtIndex:section];
        NSArray *array = [dictionary objectForKey:@"Items"];
        return [array count];
    }
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellStyleSubtitle;
    }    

    // Set up the cell...

    if(searching) {

        cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [copyListOfItemsSubtitle objectAtIndex:indexPath.row];

        NSString *nomeImmagine = [NSString stringWithFormat:@"CellImage.png"];
        [[cell imageView] setImage:[UIImage imageNamed:nomeImmagine]];
    }

    else {

        //First get the dictionary object
        NSDictionary *dictionary =[listOfItems objectAtIndex:indexPath.section];
        NSArray *array = [dictionary objectForKey:@"Items"];
        NSString *cellValue = [array objectAtIndex:indexPath.row];

        NSDictionary *subtitleDictionary =[listOfItemsSubtitle objectAtIndex:indexPath.section];
        NSArray *subtitleArray = [subtitleDictionary objectForKey:@"ItemsDetail"];
        NSString *subtitleCellValue = [subtitleArray objectAtIndex:indexPath.row];        

        cell.textLabel.text = cellValue;
        cell.detailTextLabel.text = subtitleCellValue;
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;

        NSString *nomeImmagine = [NSString stringWithFormat:@"CellImage.png"];
        [[cell imageView] setImage:[UIImage imageNamed:nomeImmagine]];

    }

    return cell;
}

// DidSelectARow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Get the Selected Item

    NSString *selectedItem = nil;
    NSString *subtitleCellValue = nil;

    if(searching) {

        selectedItem = [copyListOfItems objectAtIndex:indexPath.row];
        subtitleCellValue = [copyListOfItemsSubtitle objectAtIndex:indexPath.row];

    }

    else {

        NSDictionary *dictionary =[listOfItems objectAtIndex:indexPath.section];
        NSArray *array = [dictionary objectForKey:@"Items"];
        selectedItem = [array objectAtIndex:indexPath.row];

        NSDictionary *subtitleDictionary =[listOfItemsSubtitle objectAtIndex:indexPath.section];
        NSArray *subtitleArray = [subtitleDictionary objectForKey:@"ItemsDetail"];
        subtitleCellValue = [subtitleArray objectAtIndex:indexPath.row];

    }
}

Then in ViewDidLoad

//Initialize the copy array.
    copyListOfItems = [[NSMutableArray alloc] init];
    copyListOfItemsSubtitle = [[NSMutableArray alloc] init];

    //Add the search bar
    self.tableView.tableHeaderView = searchBar;
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

    searching = NO;
    letUserSelectRow = YES;