title: UITableView Search Causes Strange Scrolling
body: <p>I have an app where I have a UITableView displaying calendar events. I made it so that it is possible to search through the events using a search bar:</p>
  
  <pre><code>- (void)searchThroughData {
      _searchResults = [NSMutableArray array];
      _searchHeaders = [NSMutableArray array];
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title CONTAINS[c] %@", [_searchBar text]];
      for (int i = 0; i &lt; [[[[self appDelegate] user] calendarDays] count]; i++) {
          NSArray *searchedArray = [[[[[[[self appDelegate] user] calendarDays] objectAtIndex:i] calendarEvents] filteredOrderedSetUsingPredicate:predicate] array];
          if ([searchedArray count] &gt; 0) {
              [_searchResults addObject:searchedArray];
              [_searchHeaders addObject:[[[[[self appDelegate] user] calendarDays] objectAtIndex:i] dateString]];
          }
      }
  }
  
  - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
      if (![[searchBar text] isEqualToString:@""]) {
          [self searchThroughData];
      }
      [_eventsTableView reloadData];
  }
  
  -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
      [_searchBar resignFirstResponder];
  }
  -(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar {
      [_eventsTableView reloadData];
      [_searchBar resignFirstResponder];
      [_searchBar setText:@""];
  }
  
  #pragma mark - UITableView Delegate and DataSource methods
  
  - (int)numberOfSectionsInTableView:(UITableView *)tableView {
      if (
      self.eventsTableView) {
          return [[[[self appDelegate] user] calendarDays] count];
      } else {
          return [_searchHeaders count];
      }
  }
  
  - (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      if (
      self.eventsTableView) {
          return [[[[[[self appDelegate] user] calendarDays] objectAtIndex:section] calendarEvents] count];
      } else {
          return [[_searchResults objectAtIndex:section] count];
      }
  }
  
  - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      if (
      self.eventsTableView) {
          return [[[[[self appDelegate] user] calendarDays] objectAtIndex:section] dateString];
      } else {
          return [_searchHeaders objectAtIndex:section];
      }
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
  
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
          [[cell textLabel] setFont:[UIFont systemFontOfSize:15]];
      }
  
      if (
      self.eventsTableView)
          [[cell textLabel] setText:[[[[[[[self appDelegate] user] calendarDays] objectAtIndex:[indexPath section]] calendarEvents] objectAtIndex:[indexPath row]] title]];
      else
          [[cell textLabel] setText:[[[_searchResults objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] title]];
  
      if (_grayedOut)
          [[cell textLabel] setTextColor:[UIColor grayColor]];
      else
          [[cell textLabel] setTextColor:[UIColor blackColor]];
  
      return cell;
  }
  
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      [_searchBar resignFirstResponder];
      if (
      self.eventsTableView)
          [self loadEventView:[[[[[[self appDelegate] user] calendarDays] objectAtIndex:[indexPath section]] calendarEvents] objectAtIndex:[indexPath row]]];
      else
          [self loadEventView:[[_searchResults objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]]];
  
      [tableView deselectRowAtIndexPath:indexPath animated:YES];
  }
  </code></pre>
  
  <p>All looks good initially:</p>
  
  <p><img src="https://i.stack.imgur.com/83Vp6.png" alt="enter image description here"></p>
  
  <p>However when I scroll to the bottom the UITableView appears to end in a strange position:</p>
  
  <p><img src="https://i.stack.imgur.com/DjACv.png" alt="enter image description here"></p>
  
  <p>or I can scroll down infinitely with no scroll bar appearing:</p>
  
  <p><img src="https://i.stack.imgur.com/MvsNv.png" alt="enter image description here"></p>
  
  <p>Why is the UITableView acting so strangely and how can I fix this issue?</p>