开发者

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

开发者 https://www.devze.com 2023-03-26 17:42 出处:网络
I added a search bar into my TableViewController\'s header, and I\'ve also added a \"{search}\" tothe \"sectionIndexTitlesForTableView\" array. Now on the right side, I got the letters of the alphabet

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...

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

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


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


Are you dealing with xib, if yes then move you search bar top of the UITableView for example

UIView // your main view  
  SearchBar
  UITableView

Ok actually your question was not clear to me, now after you comment i expand my answer

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
      {
          // An array of strings that serve as the title of sections
      }

This datasource method just asks the data source to return the titles for the sections for a table view.

I would suggest you to refer

according to your picture i will stick again to my previous answer based on usability issue. why can not you put the search bar stick to top forever, it will just eat one/two result set row (ok here i am giving the strong suggestion- if you allow the user to goto searchbar by clicking on your searchicon is not easy(its very tiny) sectionIndex title generally user do not use and if they use they just use for to go to particular section directly).

Hope my suggestion may be the good answer for you.

Thanks


You can do it with another method of UITableView as below:

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

    if (//this condition is apply for A-Z//) {

    }
    else { //--- This is for your Search symbol---//
        [tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    } 
    return (return your desire section value i.e. 1);
}
0

精彩评论

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