Scroll to TableViewHeader using “tableView:sectionForSectionIndexTitle:atIndex:” help?

笑着哭i 提交于 2019-12-22 04:35:26

问题


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


回答1:


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




回答2:


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




回答3:


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);
}


来源:https://stackoverflow.com/questions/6994480/scroll-to-tableviewheader-using-tableviewsectionforsectionindextitleatindex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!