【问题标题】:Add UISearchBar to UITableView programmatically not working以编程方式将 UISearchBar 添加到 UITableView 不起作用
【发布时间】: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


    【解决方案1】:

    您应该改用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 无论如何都会使事情变得更简单......

    如果你想保持现在的样子,那么你可以把self.tableView = tableVew;放在[self.view addSubview:tableView];之后...

    【讨论】:

    • 有没有办法在 UIVewController 而不是 UITableViewController 中使用 UITableView 来实现这一点?我在 UIVeiwController 上有图形样式(阴影等),这在 UITableViewController 上似乎是不可能的。
    • 已经解决了,非常感谢。这也帮助我在我的代码中发现了一些需要清理的东西。
    【解决方案2】:

    问题是

    self.tableView
    

    在 loadTableView 中为 nil 或者它不是您以编程方式创建的那个表。

    添加

    self.tableView = tableView;
    

    之后

    [self.view addSubview:tableView];
    

    如果没有这个,你的 searchBar 会被添加到无效的 tableView 中。

    应该

    【讨论】:

    • 这个答案也是正确的,但我将@jjv360 标记为正确答案,因为他很好地说明了我哪里出错了。不过感谢您的宝贵时间。
    • 如果你喜欢我的答案,你可以为我的答案加 1。
    猜你喜欢
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多