单击UISearchBar中的取消按钮会导致tableView中没有选择?

时间:2012-04-20 11:10:02

标签: iphone objective-c ios

我正在使用UISearchBar,搜索工作正常但搜索后点击取消按钮,didSelectedRowAtIndexPath在UItableView中不起作用。它不允许我选择任何行进一步。

任何想法?

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{
    isSearchOn = YES;
    canSelectRow = NO;
    table.scrollEnabled = NO;
}

- (void) doneSearching:(id)sender 
{   
    isSearchOn = NO;
    canSelectRow = YES;
    table.scrollEnabled = YES;
    self.navigationItem.rightBarButtonItem = nil;

    [table reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar 
    textDidChange:(NSString *)searchText 
{
    if ([searchText length] > 0) {
        isSearchOn = YES;
        canSelectRow = YES;
        table.scrollEnabled = YES;
        [self searchMoviesTableView:searchText];
    }
    else {
        isSearchOn = NO;
        canSelectRow = NO;
        table.scrollEnabled = NO;       
    }
    [table reloadData];
}

- (void) searchMoviesTableView :(NSString*)searchText{
    [searchResult removeAllObjects];

    for (NSDictionary *artistDic in listOfMovies) {
        NSRange titleResultRange = [[artistDic valueForKey:@"artist"] rangeOfString:searchText options:(NSCaseInsensitiveSearch | NSLiteralSearch)];
        if (titleResultRange.length > 0)
            [searchResult addObject:artistDic];
    }
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.text = @"";
    table.scrollEnabled = true;
    [table reloadData];
    [searchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{
    [searchBar resignFirstResponder];
}

2 个答案:

答案 0 :(得分:1)

这是我的错误......

else {
    isSearchOn = NO;
    canSelectRow = NO;
    table.scrollEnabled = NO;       
}

set canSelectRow=YES;

答案 1 :(得分:0)

我注意到你在打电话:

searchBar.text = @"";
searchBarCancelButtonClicked:中的

,可能会导致searchBar:textDidChange:的另一次调用。在searchBar:textDidChange:中设置:

canSelectRow = NO;

表示长度为0的字符串(@""为真)。我想知道是根本原因吗?