Objective-C uisearchbar一次只允许一个字符 [英] objective-c uisearchbar only allows one character at a time

查看:85
本文介绍了Objective-C uisearchbar一次只允许一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我正在使用的一些代码.可能会帮助您解决这个问题.

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

问题似乎出在[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.

推荐答案

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

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

对我有用.

这篇关于Objective-C uisearchbar一次只允许一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆