以编程方式将UISearchBar添加到UITableView

Gar*_*ren 35 iphone interface uitableview uisearchbar

我试图添加一个UISearchBar(与其相应的搜索逻辑),但有一个问题:我使用UITableViewController自动生成的子类UITableView而不是单独的nib文件,并根据表视图以编程方式操作所有内容.

在Interface Builder中,可以选择将搜索栏搜索显示控制器添加nib.有没有办法以编程方式完成相同的任务,或者是否有利于放弃默认UITableView并转移到自定义的UITableViewnib文件,以便我可以轻松添加UISearchbar

此外,我已经尝试UITableView通过以下代码在我的viewDidLoad实现中测试只是添加一个搜索栏到我的标题(这是我想要它去的地方),但它显示在表格部分标题后面,因此除非表格滚动,否则它是不可见的向下显示否则将是白色空间.怎么了?

UISearchBar *testbar = [[UISearchBar alloc] init];

[[self tableView] setTableHeaderView:testbar];
Run Code Online (Sandbox Code Playgroud)

min*_*nux 74

要将UISearchBar添加到UITableView,可以执行以下操作:

  • 声明并初始化UISearchBar
  • 声明并初始化UISearchDisplayController
  • 将searchBar添加到tableView
  • 实现UISearchDisplayController委托
  • 在头文件中,我声明了我的searchBar,searchDisplayController以及包含要显示的数据的数组.

ViewController.h:

#import 

@interface ViewController : UITableViewController
{
    NSArray *originalData;
    NSMutableArray *searchData;

    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;
}

@end
Run Code Online (Sandbox Code Playgroud)

我还在课堂上添加了两个代表:UISearchBarDelegate和UISearchDisplayDelegate,没有,searchBar根本不起作用!

初始化数据:

//ViewController.m

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        NSArray *group1 = [[NSArray alloc] initWithObjects:@"Napoli", @"Juventus", @"Inter", @"Milan", @"Lazio", nil];
        NSArray *group2 = [[NSArray alloc] initWithObjects:@"Real Madrid", @"Barcelona", @"Villareal", @"Valencia", @"Deportivo", nil];
        NSArray *group3 = [[NSArray alloc] initWithObjects:@"Manchester City", @"Manchester United", @"Chelsea", @"Arsenal", @"Liverpool", nil];

        originalData = [[NSArray alloc] initWithObjects:group1, group2, group3, nil];
        searchData = [[NSMutableArray alloc] init];
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

现在我们需要初始化我们的两个对象,我评论了代码来解释我的工作:

//ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
/*the search bar widht must be > 1, the height must be at least 44
(the real size of the search bar)*/

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
/*contents controller is the UITableViewController, this let you to reuse
the same TableViewController Delegate method used for the main table.*/

    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
//set the delegate = self. Previously declared in ViewController.h

    self.tableView.tableHeaderView = searchBar; //this line add the searchBar
                                                //on the top of tableView.
}
Run Code Online (Sandbox Code Playgroud)

我决定跳过关于tableView单元初始化的部分,你可以在可下载的源代码中找到它.:)

当tableView准备就绪时,是时候实现UISearchDisplayControllerDelegate了!

委托有很多方法来控制搜索的每个方面,但最重要的是

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
Run Code Online (Sandbox Code Playgroud)

每次在searchBar中插入新字符时都会调用此方法,您将获取searchString,通过表元素执行搜索并返回YES.

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [searchData removeAllObjects];
    /*before starting the search is necessary to remove all elements from the
      array that will contain found items */

    NSArray *group; 

/* in this loop I search through every element (group) (see the code on top) in
the "originalData" array, if the string match, the element will be added in a
new array called newGroup. Then, if newGroup has 1 or more elements, it will be
added in the "searchData" array. shortly, I recreated the structure of the
original array "originalData". */

    for(group in originalData) //take the n group (eg. group1, group2, group3)
                               //in the original data
    {
        NSMutableArray *newGroup = [[NSMutableArray alloc] init];
        NSString *element;

        for(element in group) //take the n element in the group
        {                    //(eg. @"Napoli, @"Milan" etc.)
            NSRange range = [element rangeOfString:searchString
                                     options:NSCaseInsensitiveSearch];

            if (range.length > 0) { //if the substring match
                [newGroup addObject:element]; //add the element to group
            }
        }

        if ([newGroup count] > 0) {
            [searchData addObject:newGroup];
        }

        [newGroup release];
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

就这样!此方法将在所有元素中执行全文搜索.当搜索结束时,tableView重新加载自己.请参阅源代码以查看其他详细信息.

下载源文件(Link UPDATED!(04.01.2016))


Pra*_*n-K 21

设置UISearchBar的框架:

// Change the position according to your requirements
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 70, 320, 44)];
Run Code Online (Sandbox Code Playgroud)

如果您的视图是其子类UITableViewController,请将其更改为UIViewController.

  • 在您的nib文件中,创建一个视图并在该视图下拖动tableView,并将该视图的引用类更改为您的 UIViewController.

  • 创建IBOutletUITableView * myTableView,并把它连接到你的笔尖文件.你只需要改变你的VC文件,例如self to[self.myTableView reloadData];

现在您可以调整tableView笔尖本身的搜索栏.

UISearchDisplaycontroller有自己的UITableView显示搜索由另一个视图控制器管理的数据的结果.这里searchDisplaycontroller结合了搜索栏和tableview,以在tableview中显示结果数据,因此不需要单独的tableview.

  • 我用(0,0,320,44) (4认同)
  • 你添加了一个不必要的过程,我已经复制了他的问题,这有用:UISearchBar*testbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,170,320,44)]; [[self tableView] setTableHeaderView:testbar]; (2认同)