Skip to content

Commit

Permalink
fix bug : less than iOS11 crash
Browse files Browse the repository at this point in the history
  • Loading branch information
TalkingJourney committed Jan 19, 2018
1 parent de9109d commit 5524d3d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
15 changes: 1 addition & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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对象的索引数据源。
Expand Down
4 changes: 2 additions & 2 deletions SCIndexView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions SCIndexView/SCIndexView.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
// 索引视图的配置
@property (nonatomic, strong, readonly) SCIndexViewConfiguration *configuration;

// SCIndexView 会对 tableView 进行 strong 引用,请注意,防止“循环引用”
- (instancetype)initWithTableView:(UITableView *)tableView configuration:(SCIndexViewConfiguration *)configuration;

@end
2 changes: 1 addition & 1 deletion SCIndexView/SCIndexView.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ @interface SCIndexView ()

@property (nonatomic, strong) NSMutableArray<CATextLayer *> *subTextLayers;
@property (nonatomic, strong) UILabel *indicator;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) UITableView *tableView;

// tableView是否实现cellHeight的代理方法
@property (nonatomic, assign) BOOL tableViewHasCellHeightMethod;
Expand Down
34 changes: 26 additions & 8 deletions SCIndexView/UITableView+SCIndexView.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
#import <objc/runtime.h>
#import "SCIndexView.h"

@interface SCWeakProxy : NSObject

@property (nonatomic, weak) SCIndexView *indexView;

@end
@implementation SCWeakProxy
@end

@interface UITableView () <SCIndexViewDelegate>

@property (nonatomic, strong) SCIndexView *sc_indexView;
Expand Down Expand Up @@ -44,15 +52,22 @@ + (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;
}
}

- (void)SCIndexView_removeFromSuperview
{
if (self.sc_indexView) {
[self.sc_indexView removeFromSuperview];
self.sc_indexView = nil;
}
[self SCIndexView_removeFromSuperview];
}
Expand All @@ -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
Expand Down Expand Up @@ -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<NSString *> *)sc_indexViewDataSource
Expand All @@ -146,13 +165,12 @@ - (void)setSc_indexViewDataSource:(NSArray<NSString *> *)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;
}

Expand Down
2 changes: 2 additions & 0 deletions SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand All @@ -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)";
Expand Down

0 comments on commit 5524d3d

Please sign in to comment.