---
title: "UISearchDisplayController使用"
author: "mrjeye"
date: "2012-01-14T10:53:58Z"
tags: []
categories: ["移动开发"]
slug: "uisearchdisplaycontroller"
draft: false
lastmod: "2012-01-14T10:53:58Z"
id: 1345259
url: "https://mrjeye.iteye.com/blog/1345259"
---

用UISearchBar初始化,设置代理

```java

UISearchBar *searchBar = [[uisearchbar alloc] init];
    self.tableView.tableHeaderView = searchBar;
    [searchbar sizetofit];
    searchBar.delegate = self;
    [searchbar release];
    
    searchDisplay = [[uisearchdisplaycontroller alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplay.delegate = self;
    searchDisplay.searchResultsDataSource = self;
    searchDisplay.searchResultsDelegate = self;

```


检测当前table view,分情况选择需要显示的数据


```java

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableview isequal:self.searchdisplaycontroller.searchresultstableview]) {
        return self.searchGroupList.count;
    }
    return [self.grouplist count];
}

```


cellForRowAtIndexPath同样选择

实现

```java

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString;

```


用于重新填充搜索出的数据