ios 如何在uitableview中添加搜索栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5538355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add searchbar in uitableview?
提问by Jean-Luc Godard
I have an NSMutableArray
displayed in a UITableView
, and I have added some elements there.
我NSMutableArray
在 a 中显示了一个UITableView
,并且在那里添加了一些元素。
For example, element names are First, FirstTwo, Second, SecondTwo, Third, ThirdTwo.
例如,元素名称为 First、FirstTwo、Second、SecondTwo、Third、ThirdTwo。
Now I want to add a search bar in the screen. In that search bar when I type F, the table should only show First and FirstTwo.
现在我想在屏幕上添加一个搜索栏。在那个搜索栏中,当我输入 F 时,表格应该只显示 First 和 FirstTwo。
How should I do this?
我该怎么做?
回答by SachinVsSachin
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
回答by Joetjah
The best way to get the hang of this is by following a tutorial over hereover here.
The part you are looking for, is this:
得到的这个窍门,最好的办法是通过以下的教程在这里看过来。您正在寻找的部分是:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[tableData removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:@""]searchText==nil){
[myTableView reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in dataSource)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the names.
{
[tableData addObject:name];
}
}
counter++;
[pool release];
}
[myTableView reloadData];
}
回答by Bhupendrasingh Lohar
If you are using search bar. then You can search your contain of table view using delegate method of searchbar
如果您使用搜索栏。然后您可以使用搜索栏的委托方法搜索表格视图的包含
(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
// myArray is main array for Table View
// tempMainArray which store myArray Data
myArray = [[NSMutableArray alloc]initWithArray:tempMainArray];
NSMutableArray *searchArray = [[NSMutableArray alloc]init];
[searchArray removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:@""]|| searchText==nil){
[myArray removeAllObjects];
myArray = tempMainArray;
[_objTableView reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in myArray)
{
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the names dynamicaly.
{
[searchArray addObject:name];
}
}
counter++;
}
if (searchArray.count > 0) {
[myArray removeAllObjects];
myArray = searchArray;
[_objTableView reloadData];
}
else
{
[myArray removeAllObjects];
myArray = tempMainArray;
[_objTableView reloadData];
}
}
回答by Kashyap Bhatt
I did it myself and it is working absolutely fine.
我自己做的,它工作得很好。
Declare two mutable arrays. One containing all the data and second for storing filtered data. Here searchuser is search bar outlet. aray is main array storing all data. set searchbar capitalisation property to sentence so that this would work correctly.
声明两个可变数组。一个包含所有数据,第二个用于存储过滤后的数据。这里 searchuser 是搜索栏插座。aray 是存储所有数据的主数组。将搜索栏大写属性设置为句子,以便它可以正常工作。
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[searchuser.text lowercaseString]);
filteredarray=[[NSMutableArray alloc]init];
int l=(int)[searchuser.text length];
if (l>0)
{
NSMutableString * data=[[NSMutableString alloc]init];
int a=(int)[aray count];
for (int i=0;i<=a-1;i++)
{
data=[aray objectAtIndex:i];
if ([searchuser.text isEqualToString:data])
{
[filteredarray addObject:data];
}
if ([data length]>l)
{
NSMutableString * string=[[NSMutableString alloc]initWithString:[data substringWithRange:NSMakeRange(0, l)]];
if ([searchuser.text isEqualToString:string])
{
[filteredarray addObject:data];
}
}
}
}
else
{
filteredarray=aray;
}
[tableview reloadData];
}