Add UISearchBar to UITableView, software not working

Background: I have UIViewControllerone that, when loaded, has program code UITableViewthat gets its data from the database SQLite3. It works great.

Problem: I need to add UISearchBar(and related logic), but when I try, the UISearcBar is not displayed.

Code:

.h file:

#import <UIKit/UIKit.h>
#import "sqlite3.h"
#import "Exhibitor.h"
@interface ExhibitorViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
{
    sqlite3 *congressDB;
    NSMutableArray *searchData;
    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;
}

@property (strong, nonatomic) IBOutlet UITableView *tableView;

-(NSString *) filePath;
-(void)openDB;

@end

.m where the UISearchBar is added:

-(void)loadTableView
{
    CGRect usableSpace = [[UIScreen mainScreen] applicationFrame];
    CGFloat usableWidth = usableSpace.size.width;
    CGFloat usableHeight = usableSpace.size.height;

    UITableView *tableView = [[UITableView alloc] init];
    [tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 64)];
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;

    self.tableView.tableHeaderView = searchBar; // I think this should have loaded the searchBar but doesn't

    // [self.tableView setTableHeaderView:searchBar]; // Have also tried this
    // [self.tableView.tableHeaderView addSubview:searchBar];  // And this
    NSLog(@"searchBar = %@", searchBar);  // This shows the searchBar is an object with values
    NSLog(@"HeaderView = %@", self.tableView.tableHeaderView);  // This shows the tableHeaderView as null ?? 

}

What am I doing wrong? What do I need to do to add UISearchBarprogrammatically UITableViewin within UIVewController?

+4
source share
2 answers

Instead, you should use UITableViewController...

.h

@interface ExhibitorViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate> {
    sqlite3 *congressDB;
    NSMutableArray *searchData;
    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;
}

-(NSString *) filePath;
-(void)openDB;

@end

wow

-(void)loadTableView {

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 64)];
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;

    self.tableView.tableHeaderView = searchBar; 

}

, , , UITableViewController ...

, , self.tableView = tableVew; [self.view addSubview:tableView];...

+13

,

self.tableView

loadTableView , .

self.tableView = tableView;

[self.view addSubview:tableView];

searchBar tableView.

+3

Source: https://habr.com/ru/post/1527471/


All Articles