开发者

Adding a searchbar above a UITableview

How is a searchbar added above a UITableView? Would just the searchbar be enough or would the 开发者_C百科search bar and search display be chosen?

I want to redraw the tableview with the items that are found by the search.


UISearchDisplayController is specifically designed to solve your problem. You should be able to figure out how to use it from reading the documentation.

Of course you could just handle the UISearchBar (or even UITextField if you want to build your own search bar) yourself.

Here's some code to get you started:

- (void)viewDidLoad {
    [super viewDidLoad];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;

    self.tableView.tableHeaderView = searchBar;

    searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchController.searchResultsDataSource = self;
    searchController.searchResultsDelegate = self;
    searchController.delegate = self;
}

Pretty much all there's left to do is to implement the delegates. If you need any help with that let me know, but may I suggest you ask a new question for each problem you encounter a long the way. Of course if you leave a comment here I will take a look at it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜