objective-c uisearchbar only allows one character at a time

一个人想着一个人 提交于 2019-12-24 02:01:05

问题


I have created the following code but there's still one thing that's bugging me. For some reason, when I press any key on the keyboard, it immediately goes away, my cancel bar is disabled, but the code works and it correctly sorts and displays the cells in my table view that have those letters that the user typed in, though only one at a time. Is there something I'm missing? Thanks.

Edit:

Here's some code that I'm using. Might help figure this out.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [self headerView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return [[self headerView] frame].size.height;
}

- (UIView *)headerView
{
if (headerView)
    return headerView;

float w = [[UIScreen mainScreen] bounds].size.width;
CGRect evHeaderFrame = CGRectMake(0.0, 0.0, w, 45.0);
theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];

theSearchBar.showsCancelButton=YES;
theSearchBar.autocorrectionType=UITextAutocorrectionTypeNo;
theSearchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
theSearchBar.delegate = self;

headerView = [[UIView alloc] initWithFrame:evHeaderFrame];
[headerView addSubview:theSearchBar];

searching = NO;

return headerView;
[headerView release];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
if(searching)
    return;
searching = YES;

NSLog(@"Search Bar Text Did Begin Editing");
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
NSLog(@"Did End Editing");
}

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

[copyListOfItems removeAllObjects];

if([searchText length] > 0) {
    NSLog(@"Search Length is greater than 0");
    searching = YES;
    self.tableView.scrollEnabled = YES;
    [self searchTableView];
}
else {
    NSLog(@"Search Length is less than 1");
    searching = NO;
    self.tableView.scrollEnabled = YES;
}

[self.tableView reloadData];
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {

[self searchTableView];
}

- (void) searchTableView {

NSString *searchText = theSearchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for (int i = 0; i < [customArray count]; i++){
    NSRange titleResultsRange = [[[customArray objectAtIndex:i] objectAtIndex:0] rangeOfString:searchText options:NSCaseInsensitiveSearch];

    if (titleResultsRange.length > 0)
        //NSLog(@"%@ , %@",searchText,[customArray objectAtIndex:i]);
        [copyListOfItems addObject:[customArray objectAtIndex:i]];
}
//NSLog(@"CopyListOfItems : %@",copyListOfItems);
[searchArray release];
searchArray = nil;
}

- (void) doneSearching_Clicked:(id)sender {

theSearchBar.text = @"";
NSLog(@"RESIGN FIRST RESPONDER");
[theSearchBar resignFirstResponder];

searching = NO;
self.navigationItem.rightBarButtonItem = nil;
self.tableView.scrollEnabled = YES;

[self.tableView reloadData];
}

Looks like the problem was the [self.tableView reloadData];. Looks like I'll have to find a new way to put the search bar on the screen.


回答1:


Instead of adding it in Section Header, try adding it on the Tableview Header.

It worked for me.




回答2:


Try to NSLog(@"resign keybord"); just before [searchBar resignFirstResponder]; and see if it shows something in console. If yes, try to figure out why that function gets called. Check out connections in NIB file and also delegates.




回答3:


If I have understood your problem, the searching of the data is causing the slow down of the UIKeyboard, as for every character entered, you are immediately searching the data. Try only searching the data on return?




回答4:


This might be because you are reloading the whole TableView which holds the UISearchBar once a filtering is made.(which is made on each character change in searchBar). So probably you should limit the reload to the section which holds the data and shouldn't reload the section containing UISearchBar.

Obj C:

[tableViewOutlet reloadSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationNone];

Swift 2:

tableViewOutlet.reloadSections(NSIndexSet(index: section), withRowAnimation: .Automatic)


来源:https://stackoverflow.com/questions/7353747/objective-c-uisearchbar-only-allows-one-character-at-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!