【问题标题】:Making UITableViewIndexSearch jump to tableHeaderView instead of section 0使 UITableViewIndexSearch 跳转到 tableHeaderView 而不是第 0 节
【发布时间】:2014-11-26 18:36:30
【问题描述】:

我的表格视图的搜索栏是它的tableHeaderView(主要是因为即使在调用reloadData 时它也会保持键盘焦点)。

我也有我的右边缘字母索引列表,惊人的UITableViewIndexSearch 表示顶部的放大镜图标。但是sectionForSectionIndexTitle:atIndex: 只允许将其映射到部分索引0,它会自动滚动到表格视图的第一部分(但将我的搜索字段隐藏在它上面)。每当点击索引的放大镜图标时,显示整个 tableHeaderView 的最优雅的方式是什么?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    从 Matt Neuberg 的《Programming iOS 7》一书中,我发现了这个相当有用的技巧:

    当用户点击放大镜(索引 0)时,它会将标题滚动到视图中,我们将返回一个虚假的部分编号。

    斯威夫特:

    func tableView(tableView: UITableView, 
           sectionForSectionIndexTitle title: String,
                               atIndex index: Int) -> Int {
        if(index == 0){
            tableView.scrollRectToVisible((tableView.tableHeaderView?.frame)!, animated: false)
        }
        return index-1
    }
    

    目标-C:

    - (NSInteger)tableView:(UITableView *) 
           sectionForSectionIndexTitle:(NSString *)title
                               atIndex:(NSInteger)index {
        if(index == 0)
            [tableView scrollRectToVisible:tableView.tableHeaderView.frame animated: NO];
    
        return index-1;
    }
    

    【讨论】:

      【解决方案2】:

      显然,在我从sectionForSectionIndexTitle:atIndex: 返回零之前,我可以通过调度一个非常轻微(0.01 秒)延迟的tableView.contentOffset = CGPointZero 来实现这一点。这有点可疑,但它确实有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-05
        • 1970-01-01
        • 2018-06-21
        • 2020-04-13
        • 2021-01-13
        • 2018-12-13
        相关资源
        最近更新 更多