背景:我有一個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
中需要做些什麼?
是否有UIVewController內有一個UITableView實現這個,而不是一個UITableViewController的一種方式?我在UITeiwController上有圖形樣式(拖放陰影等),這在UITableViewController上看起來是不可能的。 – Ryan
我編輯了底部的帖子... – jjv360
這是修復它,非常感謝。這也幫助我發現了更多的東西來清理我的代碼。 – Ryan