Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to TableViewHeader using "tableView:sectionForSectionIndexTitle:atIndex:" help?

I added a search bar into my TableViewController's header, and I've also added a "{search}" to the "sectionIndexTitlesForTableView" array. Now on the right side, I got the letters of the alphabet + the search symbol on the top... to scroll to the sections of the letters (not the search field) I used this:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

    return index -1;
}

now when I click the letter A, it scrolls to the section A and so on. Only I can't make the "search symbol" scroll to the search bar in the tableView's header...

How can I do this?

Thank you in advance...

enter image description hereenter image description here

like image 642
Martin Herman Avatar asked Aug 09 '11 10:08

Martin Herman


1 Answers

If the Search bar is at the very top of the tableView, you could do something like-

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

// as the search icon is at index 0, scroll to the top
    if(index == 0)    
        [tableView scrollRectToVisible:CGRectMake(0, 0, searchBar.width, searchBar.height) animated:YES];    
    return index -1;
}

Else, if it is a section without any rows, you could do-

CGRect searchBarRect = [tableView rectForSection:searchBarIndex];
[tableView scrollRectToVisible:searchBarRect animated:YES];

HTH,

Akshay

like image 153
Akshay Avatar answered Oct 20 '22 19:10

Akshay