以编程方式将UISearchBar添加到UITableView不起作用

Aga*_*non 4 objective-c uitableview uisearchbar ios

背景:我有一个UIViewController,当加载时,以UITableView编程方式生成从SQLite3数据库获取其数据.这很好用.

问题:我需要添加一个UISearchBar(和相关的逻辑),但是当我尝试时,UISearcBar不会被渲染.

代码到目前为止:

.h文件:

#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
Run Code Online (Sandbox Code Playgroud)

添加UISearchBar的.m文件:

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

}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我需要做什么才能以UISearchBar编程方式添加到UITableView内部UIVewController

jjv*_*360 13

你应该用一个UITableViewController......

.H

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

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

@end
Run Code Online (Sandbox Code Playgroud)

.M

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

}
Run Code Online (Sandbox Code Playgroud)

问题是你正在创建一个表视图,但没有将它分配给属性,并且使用UITableViewController使事情变得更简单...

如果你想保持它现在的方式,那么你可以放在self.tableView = tableVew;[self.view addSubview:tableView];...