【问题标题】:Quick scroll in UITableView with sectionForSectionIndexTitle使用 sectionForSectionIndexTitle 在 UITableView 中快速滚动
【发布时间】:2015-11-04 10:08:21
【问题描述】:

我想通过点击UITableView 旁边的sectionIndexTitles 来允许滚动浏览我的UITableView。到目前为止,我的解决方案如下:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSIndexPath *matchingIndexPathForTitle = [self matchingIndexPathForSectionIndexTitle:title];
    if (matchingIndexPathForTitle) {
        [tableView scrollToRowAtIndexPath:matchingIndexPathForTitle
                         atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
    return 1;
}

但似乎从iOS9开始它总是滚动到UITableView的顶部

我可以保证 matchingIndexPathForTitle 是正确的(例如,第 1 节,第 22 行)并且更改为 animated:NO 并没有什么不同。

你有吗

  1. 对这种奇怪的行为有解释吗?或
  2. 对如何实现 iOS 9 的快速滚动有什么建议吗?

【问题讨论】:

    标签: ios objective-c iphone uitableview uiscrollview


    【解决方案1】:

    由于sectionForSectionIndexTitle负责滚动到右侧部分,而不是单元格,因此在此方法中返回有效的部分索引号将取消所有自定义滚动事件。

    为了滚动到一个单元格,必须阻止滚动到该部分。这可以通过返回无效的节索引来完成,例如-1

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
    {
        NSIndexPath *matchingIndexPathForTitle = [self matchingIndexPathForSectionIndexTitle:title];
        if (matchingIndexPathForTitle) {
            [tableView scrollToRowAtIndexPath:matchingIndexPathForTitle
                             atScrollPosition:UITableViewScrollPositionTop animated:YES];
        }
        return -1; // this will do the trick and not scroll to any section
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多