【发布时间】:2014-02-18 09:46:38
【问题描述】:
背景:我有一个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
添加 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 ??
}
我做错了什么?我需要做什么才能以编程方式将UISearchBar 添加到UIVewController 内的UITableView?
【问题讨论】:
标签: ios objective-c uitableview uisearchbar