casparjespersen
casparjespersen

Reputation: 3830

Adjust UISearchBar when becoming active in a UITableView

Having set viewForHeaderInSection to be a UISearchBar for section = 0, in order to have it at the top of my UITableView; I want crazy stuff to happen when this it active. Therefore, I'm thinking that I want the section to have 0 rows when the UISearchBar is inactive and then adjust accordingly to what I want, when it is active.

Therefore I have

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}

But then my table reloads the section and I lose firstResponder for the search bar. Should I follow the previous code snippet with [self.searchBar becomeFirstResponder] then my problem is still not fixed, I guess maybe because of the animation property.

What is the sexy way to fix this kind of issue? Should I use tableView:reloadRowsAtIndexPaths instead? That would seem a bit too forced. There must be a better way.

I'm guessing I'm not the first one to want to do something like this.

Upvotes: 0

Views: 336

Answers (1)

SeanT
SeanT

Reputation: 1791

I've always added my search bars as subviews of my UITableView rather than header views, and therefore they function independently of the table, so reloading your rows shouldn't affect the search bar.

Upvotes: 1

Related Questions