UISearchBar in a UITableView

后端 未结 2 1855
鱼传尺愫
鱼传尺愫 2021-02-06 17:00

I\'m trying to mimic the behaviour of a table view similar to the iPod app for Artists - it\'s a sectioned table view with a section index on the right, with a search bar at the

相关标签:
2条回答
  • 2021-02-06 17:57

    Answered my own question, but might be helpful to other beginners:

    • Put the search bar in the table view header when view loads, scroll contentOffset down the height of this search bar (typically 44 pixels), but easy to check dynamically
    • Add a search icon in the section index with "{search}"
    • Make the sectionIndexSection for the search -1
    • When handling section index touches use the 'special' index value for search "-1" (to scroll the contentOffset back to (0, 0)

    Update: use the string constant UITableViewIndexSearch string in place of the undocumented string "{search}" - i.e. return it as one of the array items when implementing:

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

    in UITableViewDataSource.

    0 讨论(0)
  • 2021-02-06 17:57

    Put the search Controller in the beginning of the tableView and try this:

    - (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;
     } 
    
    0 讨论(0)
提交回复
热议问题