【问题标题】:UISearchBar Text Not Updating List of File NamesUISearchBar 文本未更新文件名列表
【发布时间】:2020-08-11 20:05:05
【问题描述】:

在我的 TableView 类中,我使用我的应用程序的给定文件夹中的所有文件的名称填充行。我刚刚将其更改为 UISearchBar,并且我的逻辑存在一些问题。搜索栏出现,但 tableview 只是空白。

在我的 .h 文件中,我有:

 @interface DevoSongs : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchResultsUpdating >{
        NSArray *files;
        NSIndexPath *thepath;
        NSString *filenames;
        NSArray *try2;
        NSMutableArray *searchResults;
        NSString *savedSearchTerm;
        IBOutlet UITableView *tableView;


        }
        @property (nonatomic, retain) UITableView *tableView;
    @property (strong, nonatomic) UISearchController *searchController;


    @property (nonatomic, retain) NSArray *files;
    @property (nonatomic, retain) NSString *filenames;
    @property (nonatomic, retain) NSIndexPath *thepath;
    @property (nonatomic, retain) NSArray *try2;

    @property (nonatomic, retain) NSMutableArray *searchResults;
    @property (nonatomic, copy) NSString *savedSearchTerm;
    - (void)handleSearchForTerm:(NSString *)searchTerm;
    @end

For my implementation:

-(void)viewDidLoad {
    [super viewDidLoad];
    self.searchController = [[UISearchController alloc]
     initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;

    self.searchController.searchBar.delegate = self;
    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;
    [self.searchController.searchBar sizeToFit];

}
- (void)viewWillAppear:(BOOL)animated {

    NSBundle *bundle = [NSBundle mainBundle];

    self.files  = [bundle pathsForResourcesOfType:@"pdf" inDirectory:@"thepdfpowerpoints"];
    NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
    self.title = @"Devo Songs";
    self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];


    NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count]];
    for (NSString *path in self.files) {
        [names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
    }
    self.files = [names sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];


     self.tableView.delegate = self;
       self.tableView.dataSource = self;
       self.tableView.backgroundColor = [UIColor darkGrayColor];
       self.view.backgroundColor = [UIColor grayColor];
    [super viewWillAppear:animated];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.

    if (self.searchController.isActive) {
        return [searchResults count];

    }
    else {
        return [self.files count];
    }






}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *filename = [[[self.files objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension];
    NSInteger row = [indexPath row];
    NSString *contentForThisRow = nil;


        contentForThisRow = filename;

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
        [[cell textLabel] setText:contentForThisRow];
        cell.textLabel.font = [UIFont fontWithName:@"Roboto-Bold" size:38];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor blackColor];
        return cell;
    }
    else {
        [[cell textLabel] setText:contentForThisRow];
               cell.textLabel.font = [UIFont fontWithName:@"Roboto-Bold" size:22];
               cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor darkGrayColor];
        return cell;
    }
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
    [self updateSearchResultsForSearchController:self.searchController];
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString *searchString = self.searchController.searchBar.text;
    NSPredicate *resultPredicate;

        resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchString];

    searchResults = [self.files filteredArrayUsingPredicate:resultPredicate];
    NSLog(@"results %lu", (unsigned long)[self.files count]);
    [self.tableView reloadData];
}

【问题讨论】:

    标签: ios uitableview uisearchbar


    【解决方案1】:

    删除下面的代码或下面的评论

    - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
    [self updateSearchResultsForSearchController:self.searchController];}
    

    在下面调用的节目中添加了另一个 searchController 委托方法。

    - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange (NSRange)range replacementText:(NSString *)text {
     [self updateSearchResultsForSearchController:self.searchController];}
    

    这应该可行。希望对你有帮助:)

    【讨论】:

    • 我试过了,但是当我这样做时,我什至无法在搜索栏中输入任何内容。
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多