UITableView搜索导致奇怪的滚动

我有一个应用程序,我有一个UITableView显示日历事件。 我这样做是为了可以使用搜索栏搜索事件:

- (void)searchThroughData { _searchResults = [NSMutableArray array]; _searchHeaders = [NSMutableArray array]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title CONTAINS[c] %@", [_searchBar text]]; for (int i = 0; i  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]; } 

一切看起来都不错:

在此处输入图像描述

但是当我滚动到底部时,UITableView似乎以一个奇怪的位置结束:

在此处输入图像描述

或者我可以无限向下滚动,没有滚动条出现:

在此处输入图像描述

为什么UITableView表现得如此奇怪?我该如何解决这个问题?