【问题标题】:Scroll to TableViewHeader using "tableView:sectionForSectionIndexTitle:atIndex:" help?使用“tableView:sectionForSectionIndexTitle:atIndex:”帮助滚动到 TableViewHeader?
【发布时间】:2011-08-09 10:04:58
【问题描述】:

我在 TableViewController 的标题中添加了一个搜索栏,我还在“sectionIndexTitlesForTableView”数组中添加了一个“{search}”。现在在右侧,我得到了字母表中的字母 + 顶部的搜索符号...滚动到字母的部分(不是搜索字段)我使用了这个:

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

    return index -1;
}

现在,当我单击字母 A 时,它会滚动到 A 部分,依此类推。只有我不能让“搜索符号”滚动到 tableView 标题中的搜索栏...

我该怎么做?

提前谢谢你...

【问题讨论】:

    标签: iphone ios ios4 uitableview


    【解决方案1】:

    如果搜索栏位于 tableView 的最顶部,您可以执行以下操作-

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

    否则,如果它是一个没有任何行的部分,你可以这样做-

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

    HTH,

    阿克谢

    【讨论】:

    • 相应地编辑了答案。
    • 为了避免需要参考搜索栏,也可以使用 - tableView.contentOffset = CGPointMake(0, 0) 进行滚动 - 此方法的变体与动画选项是也可用,但 IMO 应该没有动画,因此按部分索引的所有其他导航都没有
    【解决方案2】:

    您是否在处理 xib,如果是,则将您移动到 ​​UITableView 的搜索栏顶部 例如

    UIView // your main view  
      SearchBar
      UITableView
    

    好吧,其实你的问题我并不清楚,现在在你发表评论后,我扩展了我的答案

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

    此数据源方法只是要求数据源返回表格视图的部分标题。

    我建议你refer

    根据您的图片,我将根据可用性问题再次坚持我之前的答案。为什么你不能把搜索栏永远放在顶部,它只会吃一/两个结果集行(好的,我在这里给出强烈的建议-如果你允许用户通过点击转到搜索栏在您的搜索图标上并不容易(它非常小)sectionIndex 标题一般用户不使用,如果他们使用他们只是用于直接转到特定部分)。

    希望我的建议能成为你的好答案。

    谢谢

    【讨论】:

    • 不,我已经以编程方式将搜索栏添加到标题中
    • 好的,那么您可以设置 UISearchBar 的框架并添加 UITableView 的顶部。 :)
    • 不,你不明白......我想滚动到它,使用右边的索引(就像你在联系人中有它一样!)..当你触摸“A”时是的,它会将您带到 A 部分....如果您触摸“T”,它会滚动到 T 部分。当我按下搜索符号时,如何让它滚动到搜索栏(我已经有了它!)右边?
    • 我们彼此不理解..我已经完成了所有工作,部分都在那里,标题中的搜索栏和侧边栏中的部分......一切正常,除非我点击侧边栏中的搜索符号,它不会带我到标题!
    • 我需要获取tableview的标题索引什么的!
    【解决方案3】:

    您可以使用 UITableView 的另一种方法,如下所示:

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

    【讨论】:

    • 对于一些具体的工作,UITableView 提供了很多方法。所以专注于它并选择你最好的。这种方法对您有好处,因为您在表格视图的标题中使用搜索栏。因此,您的搜索栏现在也是表格视图的一部分。通过使用上述方法,您可以重新定位表格视图并在单击搜索符号时显示搜索栏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    相关资源
    最近更新 更多