ios - 使用 sectionForSectionIndexTitle 在 UITableView 中快速滚动

标签 ios objective-c iphone uitableview uiscrollview

我想允许通过点击 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 的快速滚动有什么建议吗?

最佳答案

由于 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
}

关于ios - 使用 sectionForSectionIndexTitle 在 UITableView 中快速滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33518973/

相关文章:

ios - 如何检查 UICollectionViewCell 子类是否符合协议(protocol)

ios - 当我“强制退出”时如何获得关闭事件?

iphone - 在 iPhone 相机应用程序中加速或消除这种缓慢的功能

iphone - 构建通过 Web API 与服务器同步数据的 iPhone 应用程序的主要挑战是什么?

ios - CALayer 上的 hitTest - 你如何找到哪个实际层被击中了?

iOS:出现键盘时调整屏幕大小(在 HTML5 APP 中)

ios - Core Data SIGABRT 什么时候取数据?

objective-c - Helper mac app(登录项),无法与

iphone - 在Xcode 3.2上进行的构建是否可以在IOS 5上运行