title: Filtering a TableView with sections
body: <p>I try to make a little program to learn more about searching a tableview about a searchbar. Meanwhile i'm in the third week trying to do this. I found many examples in different ways in the internet. The most with a searchdisplaycontroller but this way is only for a viewcontroller. i prefer a way that also works with an uiview and so i use the searchbar delegatemethod to filter my data dictionary. Normally i try as long i get handle such things by myself. But this is my nemesis. I really can't find a way to get this problem solved. Can anyone help me with my code? 
  Here it is.   </p>
  
  <pre><code>#import "FilterDemoTableViewController.h"
  
  @implementation FilterDemoTableViewController
  
  @synthesize filteredTableData;
  @synthesize searchBar;
  @synthesize isFiltered;
  
  @synthesize tableContents;
  @synthesize Keys;
  
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      searchBar.delegate = (id)self;
  
  
      //-----------------------My TableView Data ------------------------------
  
      NSArray *array1 = [[NSArray alloc]initWithObjects:@"Berlin",@"München",@"Stuttgart",nil];
      NSArray *array2 = [[NSArray alloc]initWithObjects:@"Paris",@"Bordeaux",@"Marseille",@"Toulouse",nil];
      NSArray *array3 = [[NSArray alloc]initWithObjects:@"London",@"Portsmouth",@"Oxford",@"York",@"Dover",nil];
      NSArray *array4 = [[NSArray alloc]initWithObjects:@"Rom" ,@"Genua",@"Mailand",@"Florenz",nil];
      NSArray *array5 = [[NSArray alloc]initWithObjects:@"Madrid",@"Barcelona",@"Toledo",@"Saragossa",@"Pamplona",nil];
      NSDictionary *dictionary =[[NSDictionary alloc]initWithObjectsAndKeys:array1,@"Deutschland",array2,@"Frankreich",array3,@"Großbritannien",array4,@"Italien",array5,@"Spanien",nil];
  
      self.tableContents = dictionary;
      self.Keys = [self.tableContents allKeys];
  
      //--------------------------------------------------------------------------    
  }
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      if (self.isFiltered) {
          return [filteredTableData count];
      } else {
          return [Keys count];}
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {   
  
      NSArray *listData =[self.tableContents objectForKey:[self.Keys objectAtIndex:section]];
  
      int rowCount;
      if(self.isFiltered)
          rowCount = filteredTableData.count;
      else
          rowCount = [listData count];
  
      return rowCount;
  
  }
  
  
  - (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];
  
  
  
      NSDictionary* sectionDictionary;
  
      if (isFiltered) {
          sectionDictionary = [filteredTableData objectAtIndex:indexPath.section];
      } else {
          sectionDictionary = [self.tableContents objectForKey:[self.Keys objectAtIndex:indexPath.section]];
      }
  
      NSArray* sectionEntries = [self.tableContents objectForKey:[self.Keys objectAtIndex:indexPath.section]];
  
      cell.textLabel.text = [sectionEntries objectAtIndex:indexPath.row]; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  
      return cell;
  }
  
  
  
  -(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
  {
      if(text.length == 0)
      {
          isFiltered = FALSE;
      }
      else
      {
       //I think here is something wrong but i don't know what
  
          isFiltered = true;
  
          filteredTableData = [[NSMutableArray alloc] init];
  
          NSMutableArray *searchArray = [[NSMutableArray alloc] init];
  
          for (NSDictionary *dictionary in tableContents)    //dictionary read
          {
              NSArray *array = [dictionary objectForKey:Keys];  //section of dictionary read
              [searchArray addObjectsFromArray:array];         
          }
  
          for (NSString *sTemp in searchArray)    
          {
              NSRange titleResultsRange = [sTemp rangeOfString:text options:NSCaseInsensitiveSearch];
  
              if (titleResultsRange.length != 0)         
                  [filteredTableData addObject:sTemp];
          }
      }
      [self.tableView reloadData];
  }
  
  
  
  - (void)viewDidUnload{
      [self setSearchBar:nil];
      [super viewDidUnload];
  }
  
  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      // Return YES for supported orientations
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
  }
  
  
  @end
  </code></pre>