2014-02-18 19 views
4

背景:我有一個UIViewController,當負載,已經產生了UITableView編程方式從SQLite3數據庫獲取數據。這工作正常。添加到的UISearchBar編程的UITableView不工作

問題:我需要添加一個UISearchBar(和相關邏輯),但是當我嘗試時,UISearcBar不會被渲染。

到目前爲止的代碼:

.h文件中:出增加了的UISearchBar

#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文件:

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

} 

我在做什麼錯?如何在UIVewController內以編程方式將UISearchBar添加到UITableView中需要做些什麼?

回答

13

您應該使用UITableViewController,而不是...

.H

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

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

@end 

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

} 

問題是你正在創建一個表視圖,但不分配給該屬性,並使用UITableViewController使事情變得更簡單yway ...

如果你想保持它現在是這樣的,那麼你可以只是把self.tableView = tableVew;[self.view addSubview:tableView];後...

+1

是否有UIVewController內有一個UITableView實現這個,而不是一個UITableViewController的一種方式?我在UITeiwController上有圖形樣式(拖放陰影等),這在UITableViewController上看起來是不可能的。 – Ryan

+0

我編輯了底部的帖子... – jjv360

+0

這是修復它,非常感謝。這也幫助我發現了更多的東西來清理我的代碼。 – Ryan

3

的問題是,

self.tableView 
在loadTableView

爲零或者它不是以編程方式創建的表格。

添加

self.tableView = tableView; 

[self.view addSubview:tableView]; 

沒有這個您的搜索欄被添加到無效的tableView。

應該

+0

這個答案也是對的,但我把@ jjv360標記爲正確的答案,因爲他說明了我錯誤的地方。謝謝你的時間。 – Ryan

+0

如果您喜歡,您可以爲我的答案添加+1。 – Avt

+0

當然,對不起。現在完成;) – Ryan

相關問題