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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21849771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:28:18  来源:igfitidea点击:

Add UISearchBar to UITableView programmatically not working

iosobjective-cuitableviewuisearchbar

提问by Agamemnon

Background:I have a UIViewControllerthat, when loads, has a UITableViewgenerated programmatically that gets its data from an SQLite3database. This works fine.

背景:我有一个UIViewController,当加载时,有一个UITableView以编程方式生成的,可以从SQLite3数据库中获取数据。这工作正常。

Problem:I need to add a UISearchBar(and associated logic) but when I try, the UISearcBar is not rendered.

问题:我需要添加一个UISearchBar(和相关的逻辑)但是当我尝试时,UISercBar 没有呈现。

Code so far:

到目前为止的代码:

.h file:

.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

.m file where the UISearchBar is added:

添加 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 ?? 

}

What am I doing wrong? What do I need to do to add a UISearchBarprogrammatically to a UITableViewwithin a UIVewController?

我究竟做错了什么?我需要做什么才能以UISearchBar编程方式将 a添加到 aUITableViewUIVewController

回答by jjv360

You should use a UITableViewControllerinstead...

你应该UITableViewController改用...

.h

。H

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

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

@end

.m

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

}

The problem is you were creating a table view but not assigning it to the property, and using UITableViewControllermakes things much simpler anyway...

问题是你正在创建一个表视图但没有将它分配给属性,UITableViewController无论如何使用会使事情变得更简单......

If you want to keep it the way it is now, then you could just put self.tableView = tableVew;after [self.view addSubview:tableView];...

如果你想保持它的方式是现在,那么你可以只是把self.tableView = tableVew;[self.view addSubview:tableView];...

回答by Avt

The problem is that

问题是

self.tableView

in loadTableView is nil or it is not that table that you have created programmatically.

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

Add

添加

self.tableView = tableView;

after

[self.view addSubview:tableView];

Without this your searchBar is added to invalid tableView.

如果没有这个,您的 searchBar 将被添加到无效的 tableView。

should

应该