Objective-C uisearchbar 一次只允许一个字符
我已经创建了以下代码,但仍有一件事困扰着我。由于某种原因,当我按键盘上的任意键时,它会立即消失,我的取消栏被禁用,但代码可以工作,并且它可以正确排序并显示我的表格视图中包含用户输入的字母的单元格,虽然一次只有一个。我有什么遗漏的吗?谢谢。
编辑:
这是我正在使用的一些代码。可能有助于解决这个问题。
- (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];。看来我必须找到一种新方法将搜索栏放在屏幕上。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要将其添加到
Section Header
中,而是尝试将其添加到Tableview
标题中。这对我有用。
Instead of adding it in
Section Header
, try adding it on theTableview
Header.It worked for me.
尝试在
[searchBar resignFirstResponder];
之前使用NSLog(@"resign keybord");
并查看它是否在控制台中显示某些内容。如果是,请尝试找出调用该函数的原因。检查 NIB 文件中的连接以及委托。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.如果我理解你的问题,数据的搜索会导致 UIKeyboard 变慢,因为对于输入的每个字符,你都会立即搜索数据。尝试只搜索返回的数据?
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?
这可能是因为一旦进行过滤(在搜索栏中的每个字符更改时进行),您将重新加载包含 UISearchBar 的整个 TableView。因此,您可能应该将重新加载限制为保存数据的部分,并且不应重新加载包含 UISearchBar 的部分。
对象 C:
Swift 2:
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:
Swift 2: