UISearchBar in UITableView

I am trying to reproduce the behavior of a table view similar to the iPod app for artists - this is a partitioned view of a table with a section index on the right, with a search bar at the top, but initially hidden when the view is shown.

I use sdk 3.1.2 and IB, so I just dragged it UISearchDisplayControllerinto my NIB - it pushes everything to search. The problem arises because I am adding UISearchBarto the first section UITableView, because if I understand correctly, should I do this so that I can go to the search bar by touching the search icon in the section index directly? When the table view appears, I see the search bar, but it has changed, and now I have a white block behind the section index at the top. It does not accept the color of the environment UISearchBar, which is interestingly different from what is shown in Interface Builder.

I found a hint to add a small navigation bar to and UISearchBarin UIView, and then add it to the table view cell. This works, but the background color of the navigation bar is what you usually expect (gray), not the color as above. More interesting, if I touch the search bar to start the search, and then click "Cancel", everything is fixed. Background across the tableview cell when the search bar is the same?

+3
source share
2 answers

Answered my own question, but may be useful to other newbies:

  • , . ( 44 ), .
  • "{search}"
  • sectionIndexSection -1
  • "" "-1" ( contentOffset (0, 0)

: string UITableViewIndexSearch "{search}" - .. :

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

UITableViewDataSource.

+12

View :

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    if (index==0){
        [tableView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
        return -1;
    }
...
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        NSMutableArray * retValue =[NSMutableArray arrayWithArray:[YOUR ARRAY]];
        [retValue insertObject:UITableViewIndexSearch atIndex:0];
       return retValue;
 } 
+1

Source: https://habr.com/ru/post/1723768/


All Articles