UISearchBar:FilterContentForSearchText 不适用于 UITableView(结果未显示在表格中) [英] UISearchBar: FilterContentForSearchText not working on a UITableView (results not shown on table)
问题描述
问题部分解决,见帖子结尾:
在 UITableView
上显示搜索结果不起作用.table
保持空白,不显示任何 results
但 searching
必须工作,因为当我 search
寻找某些东西时不在 table
中,我立即得到 No Results"
文本显示在 table
上.
Showing search results on a UITableView
not working. The table
stays blank, doesn't show any results
but the searching
must work because when I search
for something not in the table
I get immediately "No Results"
text shown on the table
.
注意:在IB
中,我在Connection Inspector
中做了connect
UISearchBar
的delegate
code> 和我的 Controller
.
Note: In IB
I did connect
the delegate
of UISearchBar
in Connection Inspector
with my Controller
.
希望能帮到你.
米:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (tableView == self.searchDisplayController.searchResultsTableView) ? [self.seriesArrayFiltered count] : [self.seriesArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SeriesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Serie *aSerie = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(@"search is displaying");
aSerie = self.seriesArrayFiltered[[indexPath row]];
} else {
NSLog(@"search is NOT displaying");
aSerie = self.seriesArray[[indexPath row]];
}
UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];
valueLbl.text = aSerie.name;
return cell;
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.seriesArrayFiltered removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
self.seriesArrayFiltered = [NSMutableArray arrayWithArray:[self.seriesArray filteredArrayUsingPredicate:predicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
h:
#import <UIKit/UIKit.h>
@interface SeriesTableVC : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
@property (nonatomic, strong) NSArray *seriesArray;
@property (nonatomic, strong) NSMutableArray *seriesArrayFiltered;
@property (weak, nonatomic) IBOutlet UISearchBar *seriesSearchBar;
@end
当我更改以下代码时,我现在看到了搜索结果,但我想知道为什么无法识别标签.
在:cellForRowAtIndexPath
//UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];
//valueLbl.text = aSerie.name;
cell.textLabel.text = aSerie.name;
推荐答案
您创建的单元格不包含标签,因此
The cells that you create do not contain a label, therefore
UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];
返回nil
,设置valueLbl.text = aSerie.name;
没有效果.
您必须在创建单元格时创建标签并将其作为子视图添加到单元格中:
You have to create the label and add it as subview to the cell when you create the cell:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *valueLbl = [[UILabel alloc] initWithFrame:...];
[cell.contentView addSubview:valueLbl];
}
这篇关于UISearchBar:FilterContentForSearchText 不适用于 UITableView(结果未显示在表格中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!