Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UITableView with search bar displays incorrect results

I have a UITableView, using a custom cell with a search bar. It displays the cells fine and the search works fine, displaying the search results in the UITableVIew. When the user clicks on a cell I have it set up so that it will automatically populate the text and image on the next view controller based on what cell is selected. Again this works fine... Unless the user searches and selects the cell through the search.
If the user makes a search and then selects the cell, it displays the incorrect image and text in the detailed view controller. It seems to display whatever cell would be in that position before the search (for example, user makes a search and selects the first cell from the results - Cell 1 - It will then load the detailed view controller with whatever info was in cell 1 before the search).
I hope this makes sense....
Any ideas?
Thanks
My code for the UITableView and search is:
#import "CloudTableViewController.h"

@interface CloudTableViewController ()
{
    NSArray *cloudArray;
    NSMutableArray *filteredCloudArray;
}
@end

@implementation CloudTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    cloudArray = [NSArray arrayWithObjects:
                  [Cloud newCloud:@"Noctilucent" withClassification:@"Polar Mesopheric" withInformation:@"Text 1" withImage:@"Noctilucent.jpg"],
                  [Cloud newCloud:@"Nacreous" withClassification:@"Polar Stratospheric" withInformation:@"Text 2" withImage:@"Nacreous.JPG"],
                  [Cloud newCloud:@"Cirrus Unicinus" withClassification:@"Tropospheric" withInformation:@"Text 3" withImage:@"CirrusUncinus.jpg"],
                   [Cloud newCloud:@"Cirrocumulus Stratiformis" withClassification:@"Tropospheric" withInformation:@"Text 4" withImage:@"CirrocumulusStratiformus.JPG"],
                   [Cloud newCloud:@"Cirrostratus Nebulosus" withClassification:@"Tropospheric" withInformation:@"Text 5" withImage:@"CirrostratusNebulosus.jpg"],
                  [Cloud newCloud:@"Altocumulus Castekkabys" withClassification:@"Tropospheric" withInformation:@"Text 6" withImage:@"AltocumulusCastellanus.jpg"],
                   [Cloud newCloud:@"Stratocumulus Lenticularis" withClassification:@"Tropospheric" withInformation:@"Text 7" withImage:@"StratocumulusLenticularis.jpg"],
                  nil];
    
    filteredCloudArray =[[NSMutableArray alloc] initWithCapacity:[cloudArray count]];
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [filteredCloudArray count];
    }
    
    else
    {
        return [cloudArray count];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CloudCell";
    CloudCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if ( cell == nil )
    {
        cell = [[CloudCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    
    Cloud *cloudToDisplay = nil;
    
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cloudToDisplay = [filteredCloudArray objectAtIndex:indexPath.row];
    }
    
    else
    {
        cloudToDisplay = [cloudArray objectAtIndex:indexPath.row];
    }
    
    cell.nameLabel.text = cloudToDisplay.name;
    cell.classificationLabel.text = cloudToDisplay.classification;
    cell.imageView.image = [UIImage imageNamed:cloudToDisplay.imageFile];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    return cell;
}

/*- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        CloudDetailViewController *destViewController = segue.destinationViewController;
        destViewController.information = [cloudArray objectAtIndex:indexPath.row];
    }
}*/


#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Cloud *cloudToSend = [cloudArray objectAtIndex:indexPath.row];
    CloudDetailViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CloudDetail"];
    newViewController.imageFile = cloudToSend.imageFile;
    newViewController.information = cloudToSend.information;
    [self.navigationController pushViewController:newViewController animated:YES];
}


#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    [filteredCloudArray removeAllObjects];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText];
    filteredCloudArray = [NSMutableArray arrayWithArray:[cloudArray filteredArrayUsingPredicate:predicate]];
}


#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{   
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    
    return YES;
}


@end

And my code for the Detail view controller is:
#import "CloudDetailViewController.h"

@interface CloudDetailViewController ()

@end

@implementation CloudDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    _cloudImageView.image = [UIImage imageNamed:_imageFile];
    _informationTextView.text = _information;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Look at what you are doing in numberOfRowsInSection: and cellForRowAtIndexPath:. Now notice what you are NOT doing in didSelectRowAtIndexPath:
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • kevintarr131kevintarr131 Posts: 28New Users @
    Thanks Brian, I should have noticed that! However I have amended my code as follows so that it displays the correct array (or so I thought it would), but it still doesn't seem to work. Any other ideas? Thanks
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Cloud *cloudToDisplay = nil;
        
        if (tableView == self.searchDisplayController.searchResultsTableView)
        {
            cloudToDisplay = [filteredCloudArray objectAtIndex:indexPath.row];
        }
        
        else
        {
            cloudToDisplay = [cloudArray objectAtIndex:indexPath.row];
        }
        
        Cloud *cloudToSend = [cloudArray objectAtIndex:indexPath.row];
        CloudDetailViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CloudDetail"];
        newViewController.imageFile = cloudToSend.imageFile;
        newViewController.information = cloudToSend.information;
        [self.navigationController pushViewController:newViewController animated:YES];
        
    
    }
    
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Dude, you're really not paying attention to your code. Slow down, carefully read what you just posted.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • kevintarr131kevintarr131 Posts: 28New Users @
    Jesus.... How did I not realise that I was choosing which array to display and then all of a sudden decide to revert back to the original. Now I feel stupid :/ But all sorted....Man I'm sorry for wasting your time, but very much appreciate your help!
Sign In or Register to comment.