Skip to content

Commit

Permalink
fix bug:#20
Browse files Browse the repository at this point in the history
ScrollView嵌套多个TableView,滑动到其他TableView时,点击cell失效
  • Loading branch information
TalkingJourney committed Jun 23, 2019
1 parent 0716560 commit e00803b
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 1 deletion.
1 change: 1 addition & 0 deletions SCIndexView/SCIndexView.m
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

CGFloat space = self.configuration.indexItemRightMargin * 2;
if (point.x > self.bounds.size.width - space - self.configuration.indexItemHeight
&& point.x <= self.bounds.size.width
&& point.y > CGRectGetMinY(firstLayer.frame) - space
&& point.y < CGRectGetMaxY(lastLayer.frame) + space) {
return YES;
Expand Down
6 changes: 6 additions & 0 deletions SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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 */; };
B875D82622BFB009002C472E /* SCTableViewsController.m in Sources */ = {isa = PBXBuildFile; fileRef = B875D82522BFB009002C472E /* SCTableViewsController.m */; };
B88578D0226208CD008EF8C9 /* IgnoreSectionsIndexes.plist in Resources */ = {isa = PBXBuildFile; fileRef = B88578CF226208CD008EF8C9 /* IgnoreSectionsIndexes.plist */; };
B8ACF53A22BCCAFC006E3F09 /* SCIndexViewHeaderView.m in Sources */ = {isa = PBXBuildFile; fileRef = B8ACF53922BCCAFC006E3F09 /* SCIndexViewHeaderView.m */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -52,6 +53,8 @@
B874C547200A53CB0040DA53 /* SCIndexViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCIndexViewController.m; sourceTree = "<group>"; };
B874C54C200F83770040DA53 /* UITableView+SCIndexView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UITableView+SCIndexView.h"; sourceTree = "<group>"; };
B874C54D200F83770040DA53 /* UITableView+SCIndexView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UITableView+SCIndexView.m"; sourceTree = "<group>"; };
B875D82422BFB009002C472E /* SCTableViewsController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SCTableViewsController.h; sourceTree = "<group>"; };
B875D82522BFB009002C472E /* SCTableViewsController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCTableViewsController.m; sourceTree = "<group>"; };
B88578CF226208CD008EF8C9 /* IgnoreSectionsIndexes.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = IgnoreSectionsIndexes.plist; sourceTree = "<group>"; };
B8ACF53822BCCAFC006E3F09 /* SCIndexViewHeaderView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SCIndexViewHeaderView.h; sourceTree = "<group>"; };
B8ACF53922BCCAFC006E3F09 /* SCIndexViewHeaderView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCIndexViewHeaderView.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -101,6 +104,8 @@
B874C547200A53CB0040DA53 /* SCIndexViewController.m */,
B8ACF53822BCCAFC006E3F09 /* SCIndexViewHeaderView.h */,
B8ACF53922BCCAFC006E3F09 /* SCIndexViewHeaderView.m */,
B875D82422BFB009002C472E /* SCTableViewsController.h */,
B875D82522BFB009002C472E /* SCTableViewsController.m */,
1A3B56472009EE370073C98F /* Main.storyboard */,
1A3B564A2009EE370073C98F /* Assets.xcassets */,
1A3B564C2009EE370073C98F /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -220,6 +225,7 @@
1A3B56432009EE370073C98F /* AppDelegate.m in Sources */,
B874C548200A53CB0040DA53 /* SCIndexViewController.m in Sources */,
1A3B56722009F01F0073C98F /* SCIndexView.m in Sources */,
B875D82622BFB009002C472E /* SCTableViewsController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded</key>
<true/>
</dict>
</plist>
10 changes: 10 additions & 0 deletions SCIndexViewDemo/SCIndexViewDemo/SCTableViewsController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface SCTableViewsController : UIViewController

@end

NS_ASSUME_NONNULL_END
160 changes: 160 additions & 0 deletions SCIndexViewDemo/SCIndexViewDemo/SCTableViewsController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@

#import "SCTableViewsController.h"
#import "YYModel.h"
#import "SectionItem.h"
#import "UITableView+SCIndexView.h"
#import "SCIndexViewHeaderView.h"

@interface SCTableViewsController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITableView *indexTableView;
@property (nonatomic, strong) UITableView *otherTableView;
@property (nonatomic, copy) NSArray<SectionItem *> *indexDataSource;
@property (nonatomic, copy) NSArray<SectionItem *> *otherDataSource;

@end

@implementation SCTableViewsController

- (void)viewDidLoad {
[super viewDidLoad];

self.navigationItem.title = @"ScrollView嵌套多个TableView";
[self.view addSubview:self.scrollView];
[self.scrollView addSubview:self.indexTableView];
[self.scrollView addSubview:self.otherTableView];

CGSize size = self.view.bounds.size;
CGFloat width = size.width;
CGFloat height = size.height;

self.indexTableView.frame = CGRectMake(0, 0, width, height - 64);
self.otherTableView.frame = CGRectMake(width, 0, width, height - 64);
self.scrollView.contentSize = CGSizeMake(2 * width, height - 64);

dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSArray *indexDataSource = [self getDataSourceWithPlistName:@"Indexes"];
NSArray *otherDataSource = [self getDataSourceWithPlistName:@"IgnoreSectionsIndexes"];

NSMutableArray *indexViewDataSource = [NSMutableArray array];
NSUInteger startSection = 0;
for (SectionItem *item in indexDataSource) {
if ([item.title hasPrefix:@"Ignore"]) {
startSection++;
continue;
}
[indexViewDataSource addObject:item.title];
}
dispatch_sync(dispatch_get_main_queue(), ^{
self.indexDataSource = indexDataSource.copy;
self.otherDataSource = otherDataSource.copy;

[self.indexTableView reloadData];
[self.otherTableView reloadData];

self.indexTableView.sc_indexViewDataSource = indexViewDataSource.copy;
self.indexTableView.sc_startSection = startSection;
});
});
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.indexTableView) {
return self.indexDataSource.count;
}
else {
return self.otherDataSource.count;
}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
SectionItem *sectionItem;
if (tableView == self.indexTableView) {
sectionItem = self.indexDataSource[section];
}
else {
sectionItem = self.otherDataSource[section];
}
return sectionItem.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
SectionItem *sectionItem;
if (tableView == self.indexTableView) {
sectionItem = self.indexDataSource[indexPath.section];
}
else {
sectionItem = self.otherDataSource[indexPath.section];
}
cell.textLabel.text = sectionItem.items[indexPath.row];;
return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
SectionItem *sectionItem;
if (tableView == self.indexTableView) {
sectionItem = self.indexDataSource[section];
}
else {
sectionItem = self.otherDataSource[section];
}
return sectionItem.title;
}

#pragma mark - Private Methods

- (NSArray<SectionItem *> *)getDataSourceWithPlistName:(NSString *)plistName {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
return [NSArray yy_modelArrayWithClass:SectionItem.class json:[NSArray arrayWithContentsOfFile:plistPath]];
}

#pragma mark - Getter and Setter

- (UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.pagingEnabled = YES;
}
return _scrollView;
}

- (UITableView *)indexTableView {
if (!_indexTableView) {
_indexTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_indexTableView.dataSource = self;
_indexTableView.delegate = self;
[_indexTableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"cell"];

SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configuration];
_indexTableView.sc_indexViewConfiguration = configuration;
_indexTableView.sc_translucentForTableViewInNavigationBar = NO;
}
return _indexTableView;
}

- (UITableView *)otherTableView {
if (!_otherTableView) {
_otherTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_otherTableView.dataSource = self;
_otherTableView.delegate = self;
[_otherTableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"cell"];
}
return _otherTableView;
}

@end
16 changes: 15 additions & 1 deletion SCIndexViewDemo/SCIndexViewDemo/ViewController.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#import "ViewController.h"
#import "SCIndexViewController.h"
#import "SCTableViewsController.h"

@interface ViewController ()

Expand Down Expand Up @@ -56,6 +57,12 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
}
break;

case 4:
{
SCTableViewsController *indexViewController = [SCTableViewsController new];
viewController = indexViewController;
}

default:
break;
}
Expand All @@ -66,7 +73,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Expand Down Expand Up @@ -102,6 +109,13 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}
break;

case 4:
{
cell.textLabel.text = @"ScrollView嵌套多个TableView";
cell.detailTextLabel.text = @"";
}
break;

default:
break;
}
Expand Down

0 comments on commit e00803b

Please sign in to comment.