我试图实现一个搜索栏,但我没有运气处理这个问题.我真的很感激可以提供任何帮助.我有一个大项目,我有一个表视图,我想在它上面实现一个搜索栏,看看实时过滤.我不使用Storyboard但我使用的是XIB.我添加了以下协议:
<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchDisplayDelegate>
Run Code Online (Sandbox Code Playgroud)
我在@interface中声明了2个数组,第一个用于整个元素,第二个用于过滤的数组:
NSArray* OldList;
NSArray* filteredList;
Run Code Online (Sandbox Code Playgroud)
然后我设置了行数和节数,然后:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
myClassCell *cell = [tableView dequeueReusableCellWithIdentifier:MYCELLCLASS];
if (cell == nil)
{
cell = [myClassCell newFromNib];
}
NSMutableDictionary* elem = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
elem = [filteredList objectAtIndex:indexPath.row];
if ([elem count]+1 > indexPath.row)
[cell showValues:elem];
else
[cell showValues:nil];
}
else
{
elem = [OldList objectAtIndex:indexPath.row];
if ([elem count]+1 > indexPath.row)
[cell showValues:elem];
else
[cell showValues:nil];
}
return cell;
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = …Run Code Online (Sandbox Code Playgroud)