gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:36 24 4
gpt4 key购买 nike

我想允许通过点击 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com