diff --git a/README.md b/README.md index ca67eaf..0fed88a 100644 --- a/README.md +++ b/README.md @@ -14,22 +14,9 @@ toast效果图: # 使用方法 可以通过CocoaPods导入,支持iOS7及以上。 -pod 'SCIndexView' - -### 1.x版本 -1. 创建SCIndexViewConfiguration对象,这个对象用来控制索引的UI样式; -2. 创建SCIndexView对象,这个对象是索引视图本身,初始化方法必须传入UITableView列表,和SCIndexViewConfiguration对象; -3. 将SCIndexView索引视图添加到UITableView列表视图的父视图之中,再设置索引视图的数据源。 -``` -SCIndexViewConfiguration *indexViewConfiguration = [SCIndexViewConfiguration configuration]; -SCIndexView *indexView = [[SCIndexView alloc] initWithTableView:self.tableView configuration:indexViewConfiguration]; -indexView.translucentForTableViewInNavigationBar = self.translucent; -[self.view addSubview:indexView]; -indexView.dataSource = indexViewDataSource; -``` +pod 'SCIndexView' -### 2.x版本 1. 创建SCIndexViewConfiguration对象,这个对象用来控制索引的UI样式; 2. 设置UITableView对象的 sc_translucentForTableViewInNavigationBar 和 sc_indexViewConfiguration; 3. 再设置UITableView对象的索引数据源。 diff --git a/SCIndexView.podspec b/SCIndexView.podspec index 9a439fa..2270378 100644 --- a/SCIndexView.podspec +++ b/SCIndexView.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.name = "SCIndexView" - s.version = "2.0.1" + s.version = "2.0.2" 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.1" } + s.source = { :git => "https://github.com/TalkingJourney/SCIndexView.git", :tag => "2.0.2" } s.source_files = "SCIndexView/**/*.{h,m}" s.public_header_files = "SCIndexView/**/*.h" diff --git a/SCIndexView/SCIndexView.h b/SCIndexView/SCIndexView.h index 5c40110..5db6b66 100644 --- a/SCIndexView/SCIndexView.h +++ b/SCIndexView/SCIndexView.h @@ -44,6 +44,7 @@ // 索引视图的配置 @property (nonatomic, strong, readonly) SCIndexViewConfiguration *configuration; +// SCIndexView 会对 tableView 进行 strong 引用,请注意,防止“循环引用” - (instancetype)initWithTableView:(UITableView *)tableView configuration:(SCIndexViewConfiguration *)configuration; @end diff --git a/SCIndexView/SCIndexView.m b/SCIndexView/SCIndexView.m index f965083..c46cf3f 100644 --- a/SCIndexView/SCIndexView.m +++ b/SCIndexView/SCIndexView.m @@ -27,7 +27,7 @@ @interface SCIndexView () @property (nonatomic, strong) NSMutableArray *subTextLayers; @property (nonatomic, strong) UILabel *indicator; -@property (nonatomic, weak) UITableView *tableView; +@property (nonatomic, strong) UITableView *tableView; // tableView是否实现cellHeight的代理方法 @property (nonatomic, assign) BOOL tableViewHasCellHeightMethod; diff --git a/SCIndexView/UITableView+SCIndexView.m b/SCIndexView/UITableView+SCIndexView.m index c7a648a..5e1741a 100644 --- a/SCIndexView/UITableView+SCIndexView.m +++ b/SCIndexView/UITableView+SCIndexView.m @@ -3,6 +3,14 @@ #import #import "SCIndexView.h" +@interface SCWeakProxy : NSObject + +@property (nonatomic, weak) SCIndexView *indexView; + +@end +@implementation SCWeakProxy +@end + @interface UITableView () @property (nonatomic, strong) SCIndexView *sc_indexView; @@ -44,8 +52,14 @@ + (void)swizzledSelector:(SEL)swizzledSelector originalSelector:(SEL)originalSel - (void)SCIndexView_didMoveToSuperview { [self SCIndexView_didMoveToSuperview]; - if (self.superview && self.sc_indexView) { - [self.superview addSubview:self.sc_indexView]; + if (self.sc_indexViewDataSource.count && !self.sc_indexView && self.superview) { + SCIndexView *indexView = [[SCIndexView alloc] initWithTableView:self configuration:self.sc_indexViewConfiguration]; + indexView.translucentForTableViewInNavigationBar = self.sc_translucentForTableViewInNavigationBar; + indexView.delegate = self; + indexView.dataSource = self.sc_indexViewDataSource; + [self.superview addSubview:indexView]; + + self.sc_indexView = indexView; } } @@ -53,6 +67,7 @@ - (void)SCIndexView_removeFromSuperview { if (self.sc_indexView) { [self.sc_indexView removeFromSuperview]; + self.sc_indexView = nil; } [self SCIndexView_removeFromSuperview]; } @@ -79,14 +94,17 @@ - (NSUInteger)sectionOfIndexView:(SCIndexView *)indexView tableViewDidScroll:(UI - (SCIndexView *)sc_indexView { - return objc_getAssociatedObject(self, @selector(sc_indexView)); + SCWeakProxy *weakProxy = objc_getAssociatedObject(self, @selector(sc_indexView)); + return weakProxy.indexView; } - (void)setSc_indexView:(SCIndexView *)sc_indexView { if (self.sc_indexView == sc_indexView) return; - objc_setAssociatedObject(self, @selector(sc_indexView), sc_indexView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + SCWeakProxy *weakProxy = [SCWeakProxy new]; + weakProxy.indexView = sc_indexView; + objc_setAssociatedObject(self, @selector(sc_indexView), weakProxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (SCIndexViewConfiguration *)sc_indexViewConfiguration @@ -128,6 +146,7 @@ - (void)setSc_translucentForTableViewInNavigationBar:(BOOL)sc_translucentForTabl if (self.sc_translucentForTableViewInNavigationBar == sc_translucentForTableViewInNavigationBar) return; objc_setAssociatedObject(self, @selector(sc_translucentForTableViewInNavigationBar), @(sc_translucentForTableViewInNavigationBar), OBJC_ASSOCIATION_RETAIN_NONATOMIC); + self.sc_indexView.translucentForTableViewInNavigationBar = sc_translucentForTableViewInNavigationBar; } - (NSArray *)sc_indexViewDataSource @@ -146,13 +165,12 @@ - (void)setSc_indexViewDataSource:(NSArray *)sc_indexViewDataSource return; } - if (!self.sc_indexView) { + if (!self.sc_indexView && self.superview) { SCIndexView *indexView = [[SCIndexView alloc] initWithTableView:self configuration:self.sc_indexViewConfiguration]; indexView.translucentForTableViewInNavigationBar = self.sc_translucentForTableViewInNavigationBar; indexView.delegate = self; - if (self.superview) { - [self.superview addSubview:indexView]; - } + [self.superview addSubview:indexView]; + self.sc_indexView = indexView; } diff --git a/SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj b/SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj index bf79710..ffdecf1 100644 --- a/SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj +++ b/SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj @@ -353,6 +353,7 @@ CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 3J3Z9KKCQS; INFOPLIST_FILE = SCIndexViewDemo/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = SC.SCIndexViewDemo; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -369,6 +370,7 @@ CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 3J3Z9KKCQS; INFOPLIST_FILE = SCIndexViewDemo/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = SC.SCIndexViewDemo; PRODUCT_NAME = "$(TARGET_NAME)";