diff --git a/README.md b/README.md index 0fed88a..222ac5e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ toast效果图: 2. 当滑动UITableView列表时,另一根手指再滑动索引视图,列表滑动事件失效; 3. 当滑动索引视图时,会有指示器或者toast提示当前索引位置; 4. 当滑动索引视图时,不可以点击或者滑动UITableView列表; -5. 可以任意定制指示器、toast、索引视图的大小,文字颜色大小,间距等UI样式。 +5. 可以任意定制指示器、toast、索引视图的大小,文字颜色大小,间距等UI样式; +6. 当第一个数据为UITableViewIndexSearch时,自动添加放大镜图标。 # 使用方法 可以通过CocoaPods导入,支持iOS7及以上。 diff --git a/SCIndexView.podspec b/SCIndexView.podspec index feb9f6e..b86a7a9 100644 --- a/SCIndexView.podspec +++ b/SCIndexView.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.name = "SCIndexView" - s.version = "2.0.3" + s.version = "2.1.0" s.summary = "SCIndexView provide a index view." s.description = "SCIndexView provide a index view like Wechat. It is very easy." @@ -20,7 +20,7 @@ Pod::Spec.new do |s| s.platform = :ios, "7.0" - s.source = { :git => "https://github.com/TalkingJourney/SCIndexView.git", :tag => "2.0.3" } + s.source = { :git => "https://github.com/TalkingJourney/SCIndexView.git", :tag => "2.1.0" } s.source_files = "SCIndexView/**/*.{h,m}" s.public_header_files = "SCIndexView/**/*.h" diff --git a/SCIndexView/SCIndexView.m b/SCIndexView/SCIndexView.m index 36485e7..3282a7b 100644 --- a/SCIndexView/SCIndexView.m +++ b/SCIndexView/SCIndexView.m @@ -1,30 +1,35 @@ #import "SCIndexView.h" -static NSTimeInterval kAnimationDuration = 0.25; +#define kSCIndexViewSpace (self.configuration.indexItemHeight + self.configuration.indexItemsSpace) +#define kSCIndexViewMargin ((self.bounds.size.height - kSCIndexViewSpace * self.dataSource.count) / 2) -static void * SCIndexViewContext = &SCIndexViewContext; +static NSTimeInterval kAnimationDuration = 0.25; +static void * kSCIndexViewContext = &kSCIndexViewContext; static NSString *kSCFrameStringFromSelector = @"frame"; static NSString *kSCContentOffsetStringFromSelector = @"contentOffset"; // 根据section值获取CATextLayer的中心点y值 -static inline CGFloat SCGetTextLayerCenterY(NSUInteger section, CGFloat margin, CGFloat space) +static inline CGFloat SCGetTextLayerCenterY(NSUInteger position, CGFloat margin, CGFloat space) { - return margin + section * space; + return margin + (position + 1.0 / 2) * space; } // 根据y值获取CATextLayer的section值 -static inline NSInteger SCSectionOfTextLayerInY(CGFloat y, CGFloat margin, CGFloat space) +static inline NSInteger SCPositionOfTextLayerInY(CGFloat y, CGFloat margin, CGFloat space) { - NSUInteger bigger = (NSUInteger)ceil((y - margin) / space); + CGFloat position = (y - margin) / space - 1.0 / 2; + if (position <= 0) return 0; + NSUInteger bigger = (NSUInteger)ceil(position); NSUInteger smaller = bigger - 1; - CGFloat biggerCenterY = bigger * space + margin; - CGFloat smallerCenterY = smaller * space + margin; + CGFloat biggerCenterY = SCGetTextLayerCenterY(bigger, margin, space); + CGFloat smallerCenterY = SCGetTextLayerCenterY(smaller, margin, space); return biggerCenterY + smallerCenterY > 2 * y ? smaller : bigger; } @interface SCIndexView () +@property (nonatomic, strong) CAShapeLayer *searchLayer; @property (nonatomic, strong) NSMutableArray *subTextLayers; @property (nonatomic, strong) UILabel *indicator; @property (nonatomic, strong) UITableView *tableView; @@ -54,8 +59,8 @@ - (instancetype)initWithTableView:(UITableView *)tableView configuration:(SCInde [self addSubview:self.indicator]; - [tableView addObserver:self forKeyPath:kSCFrameStringFromSelector options:NSKeyValueObservingOptionNew context:SCIndexViewContext]; - [tableView addObserver:self forKeyPath:kSCContentOffsetStringFromSelector options:NSKeyValueObservingOptionNew context:SCIndexViewContext]; + [tableView addObserver:self forKeyPath:kSCFrameStringFromSelector options:NSKeyValueObservingOptionNew context:kSCIndexViewContext]; + [tableView addObserver:self forKeyPath:kSCContentOffsetStringFromSelector options:NSKeyValueObservingOptionNew context:kSCIndexViewContext]; } return self; } @@ -68,7 +73,18 @@ - (void)dealloc - (void)configSubLayersAndSubviews { - NSInteger countDifference = self.dataSource.count - self.subTextLayers.count; + BOOL hasSearchLayer = [self.dataSource.firstObject isEqualToString:UITableViewIndexSearch]; + NSUInteger deta = 0; + if (hasSearchLayer) { + self.searchLayer = [self createSearchLayer]; + [self.layer addSublayer:self.searchLayer]; + deta = 1; + } else if (self.searchLayer) { + [self.searchLayer removeFromSuperlayer]; + self.searchLayer = nil; + } + + NSInteger countDifference = self.dataSource.count - deta - self.subTextLayers.count; if (countDifference > 0) { for (int i = 0; i < countDifference; i++) { CATextLayer *textLayer = [CATextLayer layer]; @@ -83,15 +99,24 @@ - (void)configSubLayersAndSubviews } } - CGFloat space = self.configuration.indexItemHeight + self.configuration.indexItemsSpace / 2; - CGFloat margin = (self.bounds.size.height - space * self.dataSource.count) / 2; + CGFloat space = kSCIndexViewSpace; + CGFloat margin = kSCIndexViewMargin; [CATransaction begin]; [CATransaction setDisableActions:YES]; - for (int i = 0; i < self.dataSource.count; i++) { + + if (hasSearchLayer) { + self.searchLayer.frame = CGRectMake(self.bounds.size.width - self.configuration.indexItemRightMargin - self.configuration.indexItemHeight, SCGetTextLayerCenterY(0, margin, space) - self.configuration.indexItemHeight / 2, self.configuration.indexItemHeight, self.configuration.indexItemHeight); + self.searchLayer.cornerRadius = self.configuration.indexItemHeight / 2; + self.searchLayer.contentsScale = UIScreen.mainScreen.scale; + self.searchLayer.backgroundColor = self.configuration.indexItemBackgroundColor.CGColor; + } + + for (int i = 0; i < self.subTextLayers.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); - textLayer.string = self.dataSource[i]; + NSUInteger section = i + deta; + textLayer.frame = CGRectMake(self.bounds.size.width - self.configuration.indexItemRightMargin - self.configuration.indexItemHeight, SCGetTextLayerCenterY(section, margin, space) - self.configuration.indexItemHeight / 2, self.configuration.indexItemHeight, self.configuration.indexItemHeight); + textLayer.string = self.dataSource[section]; textLayer.fontSize = self.configuration.indexItemHeight * 0.8; textLayer.cornerRadius = self.configuration.indexItemHeight / 2; textLayer.alignmentMode = kCAAlignmentCenter; @@ -104,7 +129,7 @@ - (void)configSubLayersAndSubviews if (self.subTextLayers.count == 0) { self.currentSection = NSUIntegerMax; } else if (self.currentSection == NSUIntegerMax) { - self.currentSection = 0; + self.currentSection = self.searchLayer ? SCIndexViewSearchSection : 0; } else { self.currentSection = self.subTextLayers.count - 1; } @@ -112,50 +137,70 @@ - (void)configSubLayersAndSubviews - (void)configCurrentSection { + NSInteger currentSection = SCIndexViewInvalidSection; if (self.delegate && [self.delegate respondsToSelector:@selector(sectionOfIndexView:tableViewDidScroll:)]) { - NSUInteger currentSection = [self.delegate sectionOfIndexView:self tableViewDidScroll:self.tableView]; - if (currentSection != SCIndexViewInvalidSection) { + currentSection = [self.delegate sectionOfIndexView:self tableViewDidScroll:self.tableView]; + if ((currentSection >= 0 && currentSection != SCIndexViewInvalidSection) + || currentSection == SCIndexViewSearchSection) { self.currentSection = currentSection; return; } } - NSIndexPath *needIndexPath; + NSInteger firstVisibleSection = self.tableView.indexPathsForVisibleRows.firstObject.section; + CGFloat insetHeight = 0; if (!self.translucentForTableViewInNavigationBar) { - needIndexPath = self.tableView.indexPathsForVisibleRows.firstObject; + currentSection = firstVisibleSection; } 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]; + insetHeight = UIApplication.sharedApplication.statusBarFrame.size.height + 44; + for (NSInteger section = firstVisibleSection; section < self.subTextLayers.count; section++) { + CGRect sectionFrame = [self.tableView rectForSection:section]; + if (sectionFrame.origin.y + sectionFrame.size.height - self.tableView.contentOffset.y >= insetHeight) { + currentSection = section; break; } } } - if (!needIndexPath) return; - self.currentSection = needIndexPath.section; + if (currentSection == 0 && self.searchLayer) { + CGRect sectionFrame = [self.tableView rectForSection:currentSection]; + BOOL selectSearchLayer = (sectionFrame.origin.y - self.tableView.contentOffset.y - insetHeight) > 0; + if (selectSearchLayer) { + currentSection = SCIndexViewSearchSection; + } + } + + if (currentSection < 0 && currentSection != SCIndexViewSearchSection) return; + self.currentSection = currentSection; } #pragma mark - KVO - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - if (context != SCIndexViewContext) return; + if (context != kSCIndexViewContext) return; 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; + CGFloat space = kSCIndexViewSpace; + CGFloat margin = kSCIndexViewMargin; [CATransaction begin]; [CATransaction setDisableActions:YES]; - for (int i = 0; i < self.dataSource.count; i++) { + if (self.searchLayer) { + self.searchLayer.frame = CGRectMake(self.bounds.size.width - self.configuration.indexItemRightMargin - self.configuration.indexItemHeight, SCGetTextLayerCenterY(0, margin, space) - self.configuration.indexItemHeight / 2, self.configuration.indexItemHeight, self.configuration.indexItemHeight); + self.searchLayer.cornerRadius = self.configuration.indexItemHeight / 2; + self.searchLayer.contentsScale = UIScreen.mainScreen.scale; + self.searchLayer.backgroundColor = self.configuration.indexItemBackgroundColor.CGColor; + } + + NSInteger deta = self.searchLayer ? 1 : 0; + for (int i = 0; i < self.subTextLayers.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); + NSUInteger section = i + deta; + textLayer.frame = CGRectMake(self.bounds.size.width - self.configuration.indexItemRightMargin - self.configuration.indexItemHeight, SCGetTextLayerCenterY(section, margin, space) - self.configuration.indexItemHeight / 2, self.configuration.indexItemHeight, self.configuration.indexItemHeight); } [CATransaction commit]; } else if ([keyPath isEqualToString:kSCContentOffsetStringFromSelector]) { @@ -167,10 +212,18 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N - (void)onActionWithDidSelect { - if (self.currentSection < 0 || self.currentSection >= self.dataSource.count) return; + if ((self.currentSection < 0 && self.currentSection != SCIndexViewSearchSection) + || self.currentSection >= (NSInteger)self.subTextLayers.count) { + return; + } - NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:self.currentSection]; - [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; + if (self.currentSection == SCIndexViewSearchSection) { + CGFloat insetHeight = self.translucentForTableViewInNavigationBar ? UIApplication.sharedApplication.statusBarFrame.size.height + 44 : 0; + [self.tableView setContentOffset:CGPointMake(0, -insetHeight) animated:NO]; + } else { + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:self.currentSection]; + [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; + } if (self.isTouchingIndexView) { if (@available(iOS 10.0, *)) { @@ -218,9 +271,30 @@ - (UIBezierPath *)drawIndicatorPath return bezierPath; } +- (CAShapeLayer *)createSearchLayer +{ + CGFloat radius = self.configuration.indexItemHeight / 4; + CGFloat margin = self.configuration.indexItemHeight / 4; + CGFloat start = radius * 2.5 + margin; + CGFloat end = radius + sin(M_PI_4) * radius + margin; + UIBezierPath *path = [UIBezierPath bezierPath]; + [path moveToPoint:CGPointMake(start, start)]; + [path addLineToPoint:CGPointMake(end, end)]; + [path addArcWithCenter:CGPointMake(radius + margin, radius + margin) radius:radius startAngle:M_PI_4 endAngle:2 * M_PI + M_PI_4 clockwise:YES]; + [path closePath]; + + CAShapeLayer *layer = [CAShapeLayer layer]; + layer.fillColor = self.configuration.indexItemBackgroundColor.CGColor; + layer.strokeColor = self.configuration.indexItemTextColor.CGColor; + layer.contentsScale = [UIScreen mainScreen].scale; + layer.lineWidth = self.configuration.indexItemHeight / 12; + layer.path = path.CGPath; + return layer; +} + - (void)showIndicator:(BOOL)animated { - if (!self.indicator.hidden || self.currentSection < 0 || self.currentSection >= self.subTextLayers.count) return; + if (!self.indicator.hidden || self.currentSection < 0 || self.currentSection >= (NSInteger)self.subTextLayers.count) return; CATextLayer *textLayer = self.subTextLayers[self.currentSection]; if (self.configuration.indexViewStyle == SCIndexViewStyleDefault) { @@ -261,7 +335,7 @@ - (void)hideIndicator:(BOOL)animated - (void)refreshTextLayer:(BOOL)selected { - if (self.currentSection < 0 || self.currentSection >= self.subTextLayers.count) return; + if (self.currentSection < 0 || self.currentSection >= (NSInteger)self.subTextLayers.count) return; CATextLayer *textLayer = self.subTextLayers[self.currentSection]; UIColor *backgroundColor, *foregroundColor; @@ -287,15 +361,15 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event // 当滑动索引视图时,防止其他手指去触发事件 if (self.touchingIndexView) return YES; - CATextLayer *firstTextLayer = self.subTextLayers.firstObject; - if (!firstTextLayer) return NO; - CATextLayer *lastTextLayer = self.subTextLayers.lastObject; - if (!lastTextLayer) return NO; + CALayer *firstLayer = self.searchLayer ?: self.subTextLayers.firstObject; + if (!firstLayer) return NO; + CALayer *lastLayer = self.subTextLayers.lastObject ?: self.searchLayer; + if (!lastLayer) return NO; CGFloat space = self.configuration.indexItemRightMargin * 2; if (point.x > self.bounds.size.width - space - self.configuration.indexItemHeight - && point.y > firstTextLayer.frame.origin.y - space - && point.y < lastTextLayer.frame.origin.y + space) { + && point.y > CGRectGetMinY(firstLayer.frame) - space + && point.y < CGRectGetMaxY(lastLayer.frame) + space) { return YES; } return NO; @@ -304,12 +378,12 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { self.touchingIndexView = YES; - CGFloat space = self.configuration.indexItemHeight + self.configuration.indexItemsSpace / 2; - CGFloat margin = (self.bounds.size.height - space * self.dataSource.count) / 2; CGPoint location = [touch locationInView:self]; - NSInteger currentSection = SCSectionOfTextLayerInY(location.y, margin, space); - if (currentSection < 0 || currentSection >= self.subTextLayers.count) return YES; + NSInteger currentPosition = SCPositionOfTextLayerInY(location.y, kSCIndexViewMargin, kSCIndexViewSpace); + if (currentPosition < 0 || currentPosition >= (NSInteger)self.dataSource.count) return YES; + NSInteger deta = self.searchLayer ? 1 : 0; + NSInteger currentSection = currentPosition - deta; [self hideIndicator:NO]; self.currentSection = currentSection; [self showIndicator:YES]; @@ -323,17 +397,17 @@ - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { self.touchingIndexView = YES; - CGFloat space = self.configuration.indexItemHeight + self.configuration.indexItemsSpace / 2; - CGFloat margin = (self.bounds.size.height - space * self.dataSource.count) / 2; CGPoint location = [touch locationInView:self]; - NSInteger currentSection = SCSectionOfTextLayerInY(location.y, margin, space); + NSInteger currentPosition = SCPositionOfTextLayerInY(location.y, kSCIndexViewMargin, kSCIndexViewSpace); - NSUInteger subTextLayersCount = self.subTextLayers.count; - if (currentSection < 0) { - currentSection = 0; - } else if (currentSection >= subTextLayersCount) { - currentSection = subTextLayersCount - 1; + if (currentPosition < 0) { + currentPosition = 0; + } else if (currentPosition >= (NSInteger)self.dataSource.count) { + currentPosition = self.dataSource.count - 1; } + + NSInteger deta = self.searchLayer ? 1 : 0; + NSInteger currentSection = currentPosition - deta; if (currentSection == self.currentSection) return YES; [self hideIndicator:NO]; @@ -372,7 +446,9 @@ - (void)setDataSource:(NSArray *)dataSource - (void)setCurrentSection:(NSInteger)currentSection { - if (currentSection < 0 || currentSection >= self.dataSource.count || currentSection == _currentSection) return; + if ((currentSection < 0 && currentSection != SCIndexViewSearchSection) + || currentSection >= (NSInteger)self.subTextLayers.count + || currentSection == _currentSection) return; [self refreshTextLayer:NO]; _currentSection = currentSection; @@ -431,5 +507,5 @@ - (UIImpactFeedbackGenerator *)generator { } return _generator; } - + @end diff --git a/SCIndexView/SCIndexViewConfiguration.h b/SCIndexView/SCIndexViewConfiguration.h index 3e95aa2..e7159e6 100644 --- a/SCIndexView/SCIndexViewConfiguration.h +++ b/SCIndexView/SCIndexViewConfiguration.h @@ -2,6 +2,7 @@ #import extern const NSUInteger SCIndexViewInvalidSection; +extern const NSInteger SCIndexViewSearchSection; typedef NS_ENUM(NSUInteger, SCIndexViewStyle) { SCIndexViewStyleDefault = 0, // 指向点 diff --git a/SCIndexView/SCIndexViewConfiguration.m b/SCIndexView/SCIndexViewConfiguration.m index 2ca02eb..6f37858 100644 --- a/SCIndexView/SCIndexViewConfiguration.m +++ b/SCIndexView/SCIndexViewConfiguration.m @@ -2,6 +2,7 @@ #import "SCIndexViewConfiguration.h" const NSUInteger SCIndexViewInvalidSection = NSUIntegerMax - 1; +const NSInteger SCIndexViewSearchSection = -1; static inline UIColor *SCGetColor(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha) { @@ -65,7 +66,7 @@ + (instancetype)configurationWithIndexViewStyle:(SCIndexViewStyle)indexViewStyle indexItemSelectedTextColor:[UIColor whiteColor] indexItemHeight:15 indexItemRightMargin:5 - indexItemsSpace:5]; + indexItemsSpace:0]; } + (instancetype)configurationWithIndexViewStyle:(SCIndexViewStyle)indexViewStyle diff --git a/SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj b/SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj index ffdecf1..8700e59 100644 --- a/SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj +++ b/SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj @@ -21,7 +21,6 @@ 1A3B567F2009F2D30073C98F /* YYClassInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A3B567C2009F2D30073C98F /* YYClassInfo.m */; }; B874C548200A53CB0040DA53 /* SCIndexViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B874C547200A53CB0040DA53 /* SCIndexViewController.m */; }; B874C54E200F83770040DA53 /* UITableView+SCIndexView.m in Sources */ = {isa = PBXBuildFile; fileRef = B874C54D200F83770040DA53 /* UITableView+SCIndexView.m */; }; - B874C551200F9BE30040DA53 /* SCIndexView2Controller.m in Sources */ = {isa = PBXBuildFile; fileRef = B874C550200F9BE30040DA53 /* SCIndexView2Controller.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -51,8 +50,6 @@ B874C547200A53CB0040DA53 /* SCIndexViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCIndexViewController.m; sourceTree = ""; }; B874C54C200F83770040DA53 /* UITableView+SCIndexView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UITableView+SCIndexView.h"; sourceTree = ""; }; B874C54D200F83770040DA53 /* UITableView+SCIndexView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UITableView+SCIndexView.m"; sourceTree = ""; }; - B874C54F200F9BE30040DA53 /* SCIndexView2Controller.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SCIndexView2Controller.h; sourceTree = ""; }; - B874C550200F9BE30040DA53 /* SCIndexView2Controller.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCIndexView2Controller.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -96,8 +93,6 @@ 1A3B56452009EE370073C98F /* ViewController.m */, B874C546200A53CB0040DA53 /* SCIndexViewController.h */, B874C547200A53CB0040DA53 /* SCIndexViewController.m */, - B874C54F200F9BE30040DA53 /* SCIndexView2Controller.h */, - B874C550200F9BE30040DA53 /* SCIndexView2Controller.m */, 1A3B56472009EE370073C98F /* Main.storyboard */, 1A3B564A2009EE370073C98F /* Assets.xcassets */, 1A3B564C2009EE370073C98F /* LaunchScreen.storyboard */, @@ -164,7 +159,7 @@ TargetAttributes = { 1A3B563D2009EE370073C98F = { CreatedOnToolsVersion = 9.2; - ProvisioningStyle = Automatic; + ProvisioningStyle = Manual; }; }; }; @@ -207,7 +202,6 @@ files = ( 1A3B56772009F0B40073C98F /* SectionItem.m in Sources */, 1A3B56462009EE370073C98F /* ViewController.m in Sources */, - B874C551200F9BE30040DA53 /* SCIndexView2Controller.m in Sources */, 1A3B567E2009F2D30073C98F /* NSObject+YYModel.m in Sources */, B874C54E200F83770040DA53 /* UITableView+SCIndexView.m in Sources */, 1A3B56512009EE370073C98F /* main.m in Sources */, @@ -350,8 +344,8 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; - CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = 3J3Z9KKCQS; + CODE_SIGN_STYLE = Manual; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = SCIndexViewDemo/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; @@ -367,8 +361,8 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; - CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = 3J3Z9KKCQS; + CODE_SIGN_STYLE = Manual; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = SCIndexViewDemo/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; diff --git a/SCIndexViewDemo/SCIndexViewDemo/SCIndexView2Controller.h b/SCIndexViewDemo/SCIndexViewDemo/SCIndexView2Controller.h deleted file mode 100644 index 14d018f..0000000 --- a/SCIndexViewDemo/SCIndexViewDemo/SCIndexView2Controller.h +++ /dev/null @@ -1,9 +0,0 @@ - -#import -#import "SCIndexViewConfiguration.h" - -@interface SCIndexView2Controller : UIViewController - -@property (nonatomic, assign) SCIndexViewStyle indexViewStyle; - -@end diff --git a/SCIndexViewDemo/SCIndexViewDemo/SCIndexView2Controller.m b/SCIndexViewDemo/SCIndexViewDemo/SCIndexView2Controller.m deleted file mode 100644 index 3f04b3a..0000000 --- a/SCIndexViewDemo/SCIndexViewDemo/SCIndexView2Controller.m +++ /dev/null @@ -1,121 +0,0 @@ - -#import "SCIndexView2Controller.h" -#import "YYModel.h" -#import "SectionItem.h" -#import "UITableView+SCIndexView.h" - -@interface SCIndexView2Controller () - -@property (nonatomic, strong) UITableView *tableView; - -@property (nonatomic, copy) NSArray *tableViewDataSource; -@property (nonatomic, assign) BOOL translucent; - -@end - -@implementation SCIndexView2Controller - -#pragma mark - Life Cycle - -- (void)viewDidLoad { - [super viewDidLoad]; - self.view.backgroundColor = [UIColor whiteColor]; - switch (self.indexViewStyle) { - case SCIndexViewStyleDefault: - self.title = @"指向点类型 V2.x"; - break; - - case SCIndexViewStyleCenterToast: - self.title = @"中心提示弹层 V2.x"; - break; - - default: - break; - } - self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(onActionWithRightBarButton)]; - - self.translucent = YES; - - [self.view addSubview:self.tableView]; - - dispatch_async(dispatch_get_global_queue(0, 0), ^{ - NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Indexes" ofType:@"plist"]; - NSArray *tableViewDataSource = [NSArray yy_modelArrayWithClass:SectionItem.class json:[NSArray arrayWithContentsOfFile:plistPath]]; - - NSMutableArray *indexViewDataSource = [NSMutableArray array]; - for (SectionItem *item in tableViewDataSource) { - [indexViewDataSource addObject:item.title]; - } - dispatch_sync(dispatch_get_main_queue(), ^{ - self.tableViewDataSource = tableViewDataSource.copy; - [self.tableView reloadData]; - - self.tableView.sc_indexViewDataSource = indexViewDataSource.copy; - }); - }); -} - -#pragma mark - UITableViewDelegate - -- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath -{ - [tableView deselectRowAtIndexPath:indexPath animated:YES]; -} - -#pragma mark - UITableViewDataSource - -- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView -{ - return self.tableViewDataSource.count; -} - -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section -{ - SectionItem *sectionItem = self.tableViewDataSource[section]; - return sectionItem.items.count; -} - -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath -{ - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; - SectionItem *sectionItem = self.tableViewDataSource[indexPath.section]; - cell.textLabel.text = sectionItem.items[indexPath.row];; - return cell; -} - -- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section -{ - SectionItem *sectionItem = self.tableViewDataSource[section]; - return sectionItem.title; -} - - - -#pragma mark - Event Response - -- (void)onActionWithRightBarButton -{ - UIViewController *viewController = [UIViewController new]; - viewController.view.backgroundColor = [UIColor whiteColor]; - viewController.title = @"分享"; - [self.navigationController pushViewController:viewController animated:YES]; -} - -#pragma mark - Getter and Setter - -- (UITableView *)tableView -{ - if (!_tableView) { - CGFloat height = self.translucent ? 0 : 64; - _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, height, self.view.bounds.size.width, self.view.bounds.size.height - height) style:UITableViewStylePlain]; - _tableView.dataSource = self; - _tableView.delegate = self; - [_tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"cell"]; - - _tableView.sc_indexViewConfiguration = [SCIndexViewConfiguration configurationWithIndexViewStyle:self.indexViewStyle]; - _tableView.sc_translucentForTableViewInNavigationBar = self.translucent; - } - return _tableView; -} - -@end diff --git a/SCIndexViewDemo/SCIndexViewDemo/SCIndexViewController.h b/SCIndexViewDemo/SCIndexViewDemo/SCIndexViewController.h index e6805f9..bef6795 100644 --- a/SCIndexViewDemo/SCIndexViewDemo/SCIndexViewController.h +++ b/SCIndexViewDemo/SCIndexViewDemo/SCIndexViewController.h @@ -5,5 +5,6 @@ @interface SCIndexViewController : UIViewController @property (nonatomic, assign) SCIndexViewStyle indexViewStyle; +@property (nonatomic, assign) BOOL hasSearch; @end diff --git a/SCIndexViewDemo/SCIndexViewDemo/SCIndexViewController.m b/SCIndexViewDemo/SCIndexViewDemo/SCIndexViewController.m index ee5578b..d1ef7eb 100644 --- a/SCIndexViewDemo/SCIndexViewDemo/SCIndexViewController.m +++ b/SCIndexViewDemo/SCIndexViewDemo/SCIndexViewController.m @@ -2,12 +2,11 @@ #import "SCIndexViewController.h" #import "YYModel.h" #import "SectionItem.h" -#import "SCIndexView.h" +#import "UITableView+SCIndexView.h" -@interface SCIndexViewController () +@interface SCIndexViewController () @property (nonatomic, strong) UITableView *tableView; -@property (nonatomic, strong) SCIndexView *indexView; @property (nonatomic, copy) NSArray *tableViewDataSource; @property (nonatomic, assign) BOOL translucent; @@ -23,13 +22,13 @@ - (void)viewDidLoad { self.view.backgroundColor = [UIColor whiteColor]; switch (self.indexViewStyle) { case SCIndexViewStyleDefault: - self.title = @"指向点类型 V1.x"; + self.title = @"指向点类型"; break; - + case SCIndexViewStyleCenterToast: - self.title = @"中心提示弹层 V1.x"; + self.title = @"中心提示弹层"; break; - + default: break; } @@ -38,7 +37,6 @@ - (void)viewDidLoad { self.translucent = YES; [self.view addSubview:self.tableView]; - [self.view addSubview:self.indexView]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Indexes" ofType:@"plist"]; @@ -52,7 +50,10 @@ - (void)viewDidLoad { self.tableViewDataSource = tableViewDataSource.copy; [self.tableView reloadData]; - self.indexView.dataSource = indexViewDataSource.copy; + if (self.hasSearch) { + [indexViewDataSource insertObject:UITableViewIndexSearch atIndex:0]; + } + self.tableView.sc_indexViewDataSource = indexViewDataSource.copy; }); }); } @@ -91,13 +92,6 @@ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInte return sectionItem.title; } -#pragma mark - SCIndexViewDelegate - -- (void)indexView:(SCIndexView *)indexView didSelectAtSection:(NSUInteger)section -{ - -} - #pragma mark - Event Response - (void)onActionWithRightBarButton @@ -118,18 +112,16 @@ - (UITableView *)tableView _tableView.dataSource = self; _tableView.delegate = self; [_tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"cell"]; + + if (self.hasSearch) { + self.tableView.tableHeaderView = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 60)]; + } + + SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configurationWithIndexViewStyle:self.indexViewStyle]; + _tableView.sc_indexViewConfiguration = configuration; + _tableView.sc_translucentForTableViewInNavigationBar = self.translucent; } return _tableView; } -- (SCIndexView *)indexView -{ - if (!_indexView) { - _indexView = [[SCIndexView alloc] initWithTableView:self.tableView configuration:[SCIndexViewConfiguration configurationWithIndexViewStyle:self.indexViewStyle]]; - _indexView.translucentForTableViewInNavigationBar = self.translucent; - _indexView.delegate = self; - } - return _indexView; -} - @end diff --git a/SCIndexViewDemo/SCIndexViewDemo/ViewController.m b/SCIndexViewDemo/SCIndexViewDemo/ViewController.m index d66db17..ac722b3 100644 --- a/SCIndexViewDemo/SCIndexViewDemo/ViewController.m +++ b/SCIndexViewDemo/SCIndexViewDemo/ViewController.m @@ -1,7 +1,6 @@ #import "ViewController.h" #import "SCIndexViewController.h" -#import "SCIndexView2Controller.h" @interface ViewController () @@ -27,6 +26,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath { SCIndexViewController *indexViewController = [SCIndexViewController new]; indexViewController.indexViewStyle = SCIndexViewStyleDefault; + indexViewController.hasSearch = YES; viewController = indexViewController; } break; @@ -35,13 +35,14 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath { SCIndexViewController *indexViewController = [SCIndexViewController new]; indexViewController.indexViewStyle = SCIndexViewStyleCenterToast; + indexViewController.hasSearch = YES; viewController = indexViewController; } break; case 2: { - SCIndexView2Controller *indexViewController = [SCIndexView2Controller new]; + SCIndexViewController *indexViewController = [SCIndexViewController new]; indexViewController.indexViewStyle = SCIndexViewStyleDefault; viewController = indexViewController; } @@ -49,7 +50,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath case 3: { - SCIndexView2Controller *indexViewController = [SCIndexView2Controller new]; + SCIndexViewController *indexViewController = [SCIndexViewController new]; indexViewController.indexViewStyle = SCIndexViewStyleCenterToast; viewController = indexViewController; } @@ -71,32 +72,33 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil]; + cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; switch (indexPath.row) { case 0: { cell.textLabel.text = @"指向点类型"; - cell.detailTextLabel.text = @"V1.x"; + cell.detailTextLabel.text = @"有搜索"; } break; case 1: { cell.textLabel.text = @"中心提示弹层"; - cell.detailTextLabel.text = @"V1.x"; + cell.detailTextLabel.text = @"有搜索"; } break; case 2: { cell.textLabel.text = @"指向点类型"; - cell.detailTextLabel.text = @"V2.x"; + cell.detailTextLabel.text = @"无搜索"; } break; case 3: { cell.textLabel.text = @"中心提示弹层"; - cell.detailTextLabel.text = @"V2.x"; + cell.detailTextLabel.text = @"无搜索"; } break;