UITableView搜索导致奇怪的滚动

时间:2013-11-25 00:33:30

标签: ios objective-c uitableview

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

- (void)searchThroughData {
    _searchResults = [NSMutableArray array];
    _searchHeaders = [NSMutableArray array];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title CONTAINS[c] %@", [_searchBar text]];
    for (int i = 0; i < [[[[self appDelegate] user] calendarDays] count]; i++) {
        NSArray *searchedArray = [[[[[[[self appDelegate] user] calendarDays] objectAtIndex:i] calendarEvents] filteredOrderedSetUsingPredicate:predicate] array];
        if ([searchedArray count] > 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];
}

最初看起来都不错:

enter image description here

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

enter image description here

或者我可以无限向下滚动而不显示滚动条:

enter image description here

为什么UITableView的行为如此奇怪,我该如何解决这个问题呢?

0 个答案:

没有答案