UISearchBar: FilterContentForSearchText not working on a UITableView (results not shown on table)

坚强是说给别人听的谎言 提交于 2019-12-25 06:22:27

问题


PROBLEM PARTLY SOLVED, SEE END OF POST:

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.

Note: In IB I did connect the delegate of UISearchBar in Connection Inspector with my Controller.

Hope you can help.

m:

- (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

When I change the following code I see the search results now but I like to know why the label is not recognized.

In: cellForRowAtIndexPath

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

cell.textLabel.text = aSerie.name;

回答1:


The cells that you create do not contain a label, therefore

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

returns nil, and setting valueLbl.text = aSerie.name; has no effect.

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];
}


来源:https://stackoverflow.com/questions/15113714/uisearchbar-filtercontentforsearchtext-not-working-on-a-uitableview-results-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!