UISearchBar:FilterContentForSearchText无法在UITableView上工作(结果未在表格中显示)

时间:2013-02-27 13:48:57

标签: uitableview nspredicate uisearchdisplaycontroller

问题部分已解决,请看结束后发布:

UITableView上显示搜索结果无效。 table保持空白,不会显示任何results,但searching必须有效,因为当我search处于table以外的某些内容时我会立即"No Results" table {1}}上显示的{1}}文字。

注意:在IB connect delegate UISearchBar Connection Inspector 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; } #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 。{/ 1}

希望你能提供帮助。

米:

cellForRowAtIndexPath

H:

//UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];
//valueLbl.text = aSerie.name;

cell.textLabel.text = aSerie.name;

当我更改以下代码时,我现在看到了搜索结果,但我想知道 为什么标签不被识别。

在:{{1}}

{{1}}

1 个答案:

答案 0 :(得分:0)

您创建的单元格不包含标签,因此

UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];

返回nil,设置valueLbl.text = aSerie.name;无效。

创建单元格时,您必须创建标签并将其作为子视图添加到单元格中:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    UILabel *valueLbl = [[UILabel alloc] initWithFrame:...];
    [cell.contentView addSubview:valueLbl];
}