2015-11-04 117 views
2

我想允许通过点击UITableView侧的sectionIndexTitles来滚动我的UITableView。到目前为止,我的解决方案如下:使用sectionForSectionIndexTitle在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的快速滚动?

回答

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 
}