From b779889df57749dc2e3f242b68f68e5c38ae23f0 Mon Sep 17 00:00:00 2001 From: TalkingJourney Date: Mon, 15 Jan 2018 16:21:31 +0800 Subject: [PATCH] fix bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug1:修改tableView的frame,indexView的frame不正确; Bug2:重新设置indexView数据源,indexView索引位置不正确。 --- SCIndexView/SCIndexView.m | 68 +++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/SCIndexView/SCIndexView.m b/SCIndexView/SCIndexView.m index 78b0d8f..43ee7db 100644 --- a/SCIndexView/SCIndexView.m +++ b/SCIndexView/SCIndexView.m @@ -82,11 +82,11 @@ - (void)configSubLayersAndSubviews CGFloat space = self.configuration.indexItemHeight + self.configuration.indexItemsSpace / 2; CGFloat margin = (self.bounds.size.height - space * self.dataSource.count) / 2; + + [CATransaction begin]; + [CATransaction setDisableActions:YES]; for (int i = 0; i < self.dataSource.count; i++) { CATextLayer *textLayer = self.subTextLayers[i]; - - [CATransaction begin]; - [CATransaction setDisableActions:YES]; textLayer.frame = CGRectMake(self.bounds.size.width - self.configuration.indexItemRightMargin - self.configuration.indexItemHeight, SCGetTextLayerCenterY(i, margin, space), self.configuration.indexItemHeight, self.configuration.indexItemHeight); textLayer.string = self.dataSource[i]; textLayer.fontSize = self.configuration.indexItemHeight * 0.8; @@ -95,8 +95,8 @@ - (void)configSubLayersAndSubviews textLayer.contentsScale = UIScreen.mainScreen.scale; textLayer.backgroundColor = self.configuration.indexItemBackgroundColor.CGColor; textLayer.foregroundColor = self.configuration.indexItemTextColor.CGColor; - [CATransaction commit]; } + [CATransaction commit]; if (self.subTextLayers.count == 0) { self.currentSection = NSUIntegerMax; @@ -107,6 +107,31 @@ - (void)configSubLayersAndSubviews } } +- (void)configCurrentSection +{ + if (self.delegate && [self.delegate respondsToSelector:@selector(sectionOfIndexView:tableViewDidScroll:)]) { + self.currentSection = [self.delegate sectionOfIndexView:self tableViewDidScroll:self.tableView]; + return; + } + + NSIndexPath *needIndexPath; + if (!self.translucentForTableViewInNavigationBar) { + needIndexPath = self.tableView.indexPathsForVisibleRows.firstObject; + } else { + CGFloat insetHeight = UIApplication.sharedApplication.statusBarFrame.size.height + 44; + for (UITableViewCell *cell in self.tableView.visibleCells) { + CGRect frame = [cell.superview convertRect:cell.frame toView:UIApplication.sharedApplication.delegate.window]; + if (frame.origin.y + frame.size.height >= insetHeight) { + needIndexPath = [self.tableView indexPathForCell:cell]; + break; + } + } + } + + if (!needIndexPath) return; + self.currentSection = needIndexPath.section; +} + #pragma mark - KVO - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context @@ -116,6 +141,17 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N if ([keyPath isEqualToString:kSCFrameStringFromSelector]) { CGRect frame = [change[NSKeyValueChangeNewKey] CGRectValue]; self.frame = frame; + + CGFloat space = self.configuration.indexItemHeight + self.configuration.indexItemsSpace / 2; + CGFloat margin = (self.bounds.size.height - space * self.dataSource.count) / 2; + + [CATransaction begin]; + [CATransaction setDisableActions:YES]; + for (int i = 0; i < self.dataSource.count; i++) { + CATextLayer *textLayer = self.subTextLayers[i]; + textLayer.frame = CGRectMake(self.bounds.size.width - self.configuration.indexItemRightMargin - self.configuration.indexItemHeight, SCGetTextLayerCenterY(i, margin, space), self.configuration.indexItemHeight, self.configuration.indexItemHeight); + } + [CATransaction commit]; } else if ([keyPath isEqualToString:kSCContentOffsetStringFromSelector]) { [self onActionWithScroll]; } @@ -145,27 +181,7 @@ - (void)onActionWithScroll BOOL isScrolling = self.tableView.isDragging || self.tableView.isDecelerating; if (!isScrolling) return; - if (self.delegate && [self.delegate respondsToSelector:@selector(sectionOfIndexView:tableViewDidScroll:)]) { - self.currentSection = [self.delegate sectionOfIndexView:self tableViewDidScroll:self.tableView]; - return; - } - - NSIndexPath *needIndexPath; - if (!self.translucentForTableViewInNavigationBar) { - needIndexPath = self.tableView.indexPathsForVisibleRows.firstObject; - } else { - CGFloat insetHeight = UIApplication.sharedApplication.statusBarFrame.size.height + 44; - for (UITableViewCell *cell in self.tableView.visibleCells) { - CGRect frame = [cell.superview convertRect:cell.frame toView:UIApplication.sharedApplication.delegate.window]; - if (frame.origin.y + frame.size.height >= insetHeight) { - needIndexPath = [self.tableView indexPathForCell:cell]; - break; - } - } - } - - if (!needIndexPath) return; - self.currentSection = needIndexPath.section; + [self configCurrentSection]; } #pragma mark - Display @@ -337,7 +353,9 @@ - (void)setDataSource:(NSArray *)dataSource if (_dataSource == dataSource) return; _dataSource = dataSource.copy; + [self configSubLayersAndSubviews]; + [self configCurrentSection]; } - (void)setCurrentSection:(NSInteger)currentSection