使用UISearchBar搜索表视图 [英] Searching a table view with UISearchBar
问题描述
我正在使用以下代码在UItableView中进行搜索:
I am using this code to search through a UItableView:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if(searchText.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = TRUE;
if (filteredTableData == nil)
filteredTableData = [[NSMutableArray alloc] init];
else
[filteredTableData removeAllObjects];
for (NSString* string in self.itemArray)
{
NSRange nameRange = [string rangeOfString:searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if(nameRange.location != NSNotFound)
{
[filteredTableData addObject:string];
}
}
}
[tableView reloadData];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}
一切正常,但是如果我按开始编辑时出现的取消按钮,则列表不会再显示,但搜索结果仍然存在.要显示列表,我必须开始输入,甚至在搜索栏中输入单个字符,然后将其删除或按"x"查看所有列表.有没有办法阻止取消按钮?还是要在按下列表时显示列表?
Everything works fine, but if I press the cancel button which appears when I start editing, my list doesn't come back, but the search results remain. To show back the list, I have to begin typing, even a single character in the searchbar and then delete it or press the "x" to see all the list. Is there a way to block the cancel button? Or to show the list when it is pressed?
推荐答案
实现-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar UISearchBar的委托方法.在该表中,将所有对象(表中的初始对象)添加到filteredTableData数组中,然后重新加载表.
Implement - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar delegate method of UISearchBar. In that add all objects(initial objects in table) in filteredTableData array and reload table.
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[filteredTableData removeAllObjects];
[filteredTableData addObjectsFromArray:self.itemArray];
[tableView reloadData];
}
如果您使用它来选择数据源数组以重载表(在表视图数据源方法中),则也不需要维护isFiltered标志.例如
Also you don't need to maintain flag isFiltered if you are using it for choosing data source array for reloading table(in table view data source methods). e.g.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFiltered)
return filteredTableData.count
else
return self.itemArray.count
}
如果您一直都在filteredTableData数组中维护数据,则无需执行此操作.您的方法看起来像-
If you maintain data in filteredTableData array all time, you don't need to do this. Your method will look like-
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return filteredTableData.count
}
最初在控制器的init或viewDidLoad方法中,将所有对象添加到filteredTableData中,并仅使用此数组重新加载表.
Initially in init or viewDidLoad method of controller add all objects in filteredTableData and use only this array for reloading table.
因此您的方法将如下所示-
Hence your method will look like-
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0)
{
[filteredTableData removeAllObjects];
[filteredTableData addObjectsFromArray:self.itemArray];
}
else
{
[filteredTableData removeAllObjects];
for (NSString* string in self.itemArray)
{
NSRange nameRange = [string rangeOfString:searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if(nameRange.location != NSNotFound)
{
[filteredTableData addObject:string];
}
}
}
[tableView reloadData];
}
这篇关于使用UISearchBar搜索表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!