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

4

背景:

我有一个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 ?? 

}

我做错了什么?我需要怎么样才能在UIVewController中以编程方式将UISearchBar添加到UITableView中呢?

2个回答

13

你应该使用 UITableViewController 来代替...

.h

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

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

@end

.m

:这是一个在IT技术中常用的文件扩展名,通常用于Matlab程序文件。
-(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.view addSubview:tableView]; 后面添加 self.tableView = tableView; 就可以了...


1
有没有一种方法可以在UIVewController中使用UITableView来实现这个功能,而不是使用UITableViewController?我在UIVeiwController上有图形样式(下拉阴影等),在UITableViewController上似乎不可能实现。 - Agamemnon
已经解决了,非常感谢。这也帮助我发现了更多需要清理的代码。 - Agamemnon

3
问题在于:
self.tableView

在loadTableView中,可能是nil,或者不是你通过编程创建的那个表格。

添加

self.tableView = tableView;

之后

[self.view addSubview:tableView];

如果没有这个,你的搜索栏会添加到无效的表格视图中。

应该


这个答案也是正确的,但我将@jjv360标记为正确答案,因为他很好地说明了我的错误之处。谢谢你的时间。 - Agamemnon
如果您喜欢我的回答,可以为其点赞+1。 - Avt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接