【问题标题】:objective-c uisearchbar only allows one character at a timeobjective-c uisearchbar 一次只允许一个字符
【发布时间】:2011-11-13 07:15:18
【问题描述】:

我创建了以下代码,但仍有一件事困扰着我。出于某种原因,当我按下键盘上的任何键时,它会立即消失,我的取消栏被禁用,但代码可以正常工作,并且它可以正确排序并显示我的表格视图中包含用户输入的字母的单元格,虽然一次只有一个。有什么我想念的吗?谢谢。

编辑:

这是我正在使用的一些代码。可能有助于解决这个问题。

- (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];
}

看起来问题出在 [self.tableView reloadData]; 上。看来我得想办法把搜索栏放到屏幕上了。

【问题讨论】:

    标签: objective-c keyboard uisearchbar


    【解决方案1】:

    不要将其添加到 Section Header 中,而是尝试将其添加到 Tableview 标头中。

    它对我有用。

    【讨论】:

    • 最好将其作为部分标题,但是,当您向下滚动表格视图时,搜索栏会锚定到可见表格单元格的顶部。
    【解决方案2】:

    [searchBar resignFirstResponder]; 之前尝试NSLog(@"resign keybord");,看看它是否在控制台中显示了一些东西。如果是,请尝试找出调用该函数的原因。查看 NIB 文件中的连接以及委托。

    【讨论】:

    • 试过了,似乎什么也没告诉我。我将我的代码添加到我的原始帖子中。
    • 您的 .h 文件中有 吗? @interface ClassName 之类的东西: NSObject{... 只是猜测。
    • @interface RootViewController : UITableViewController {
    • 是的,我的 .h 文件中确实有该委托。
    【解决方案3】:

    如果我理解你的问题,数据的搜索导致 UIKeyboard 变慢,对于输入的每个字符,你都会立即搜索数据。尝试只搜索返回的数据?

    【讨论】:

    • 嗯,减速可能是个问题,但我真的希望它在用户输入搜索文本时更新。
    • 我发现即使我删除了搜索数据的代码,它仍然只允许我一次输入一个字符。
    【解决方案4】:

    这可能是因为一旦进行过滤,您正在重新加载包含 UISearchBar 的整个 TableView。(这是在 searchBar 中的每个字符更改时进行的)。因此,您可能应该将重新加载限制在包含数据的部分,并且不应该重新加载包含 UISearchBar 的部分。

    对象 C:

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

    斯威夫特 2:

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

    【讨论】:

      猜你喜欢
      • 2017-12-09
      • 1970-01-01
      • 2022-01-24
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多