leftViewController.h
Objective-C 1 2 3 #import @interface leftViewController : UIViewController @endleftViewController.m
Objective-C 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 #import "leftViewController.h" #import "rightViewController.h" @interface leftViewController ( ) @property ( nonatomic , strong ) UITableView *leftTableView ; @property ( nonatomic , strong ) NSArray *leftDataArr ; @property ( nonatomic , strong ) rightViewController *rightVC ; @end @implementation leftViewController - ( void ) viewDidLoad { [ super viewDidLoad ] ; // Do any additional setup after loading the view. self . view . backgroundColor = [ UIColor whiteColor ] ; [ self . view addSubview :self . leftTableView ] ; [ self creatRightVC ] ; } - ( void ) creatRightVC { self . rightVC = [ rightViewController new ] ; self . rightVC . delegate = self ; [ self addChildViewController : _rightVC ] ; [ self . view addSubview : _rightVC . view ] ; } - ( UITableView * ) leftTableView { if ( ! _leftTableView ) { _leftTableView = [ [ UITableView alloc ] initWithFrame :CGRectMake ( 0 , 0 , 120 , self . view . frame . size . height ) style :UITableViewStylePlain ] ; _leftTableView . showsVerticalScrollIndicator = NO ; _leftTableView . delegate = self ; _leftTableView . dataSource = self ; [ _leftTableView registerClass : [ UITableViewCell class ] forCellReuseIdentifier : @"cell11" ] ; } return _leftTableView ; } - ( NSArray * ) leftDataArr { if ( ! _leftDataArr ) { _leftDataArr = @ [ @"第一类" , @"第二类" , @"第三类" , @"第四类" , @"第五类" , @"第六类" , @"第七类" , @"第八类" , @"第九类" , @"第十类" , @"第十一类" , @"第十二类" , @"第十三类" , @"第十四类" , @"第十五类" , @"第十六类" , @"第十七类" , @"第十八类" , @"第十九类" , @"第二十类" , @"第二十一类" ] ; } return _leftDataArr ; } - ( NSInteger ) numberOfSectionsInTableView : ( UITableView * ) tableView { return 1 ; } - ( NSInteger ) tableView : ( UITableView * ) tableView numberOfRowsInSection : ( NSInteger ) section { return self . leftDataArr . count ; } - ( UITableViewCell * ) tableView : ( UITableView * ) tableView cellForRowAtIndexPath : ( NSIndexPath * ) indexPath { UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier : @"cell11" forIndexPath :indexPath ] ; cell . textLabel . text = self . leftDataArr [ indexPath . row ] ; return cell ; } - ( void ) tableView : ( UITableView * ) tableView didSelectRowAtIndexPath : ( NSIndexPath * ) indexPath { NSLog ( @"-----%ld" , indexPath . row ) ; if ( self . rightVC ) { [ self . rightVC scrollToSelectIndexPath :indexPath ] ; } } /* 代理右边选择时执行代理. */ - ( void ) rightViewControllerDelegate : ( rightViewController * ) vc withIndexPatch : ( NSIndexPath * ) ptch { [ self . leftTableView selectRowAtIndexPath : [ NSIndexPath indexPathForRow :ptch . section inSection : 0 ] animated :YES scrollPosition :UITableViewScrollPositionMiddle ] ; } - ( void ) rightViewControllerDelegateHeaderViewAppear : ( rightViewController * ) vc withIndexPatch : ( NSInteger ) section { [ self . leftTableView selectRowAtIndexPath : [ NSIndexPath indexPathForRow :section inSection : 0 ] animated :YES scrollPosition :UITableViewScrollPositionMiddle ] ; } - ( void ) rightViewControllerDelegateHeaderViewdisappear : ( rightViewController * ) vc withIndexPatch : ( NSInteger ) section { [ self . leftTableView selectRowAtIndexPath : [ NSIndexPath indexPathForRow :section inSection : 0 ] animated :YES scrollPosition :UITableViewScrollPositionMiddle ] ; } - ( void ) didReceiveMemoryWarning { [ super didReceiveMemoryWarning ] ; // Dispose of any resources that can be recreated. } @endrightViewController.h
Objective-C 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #import @class rightViewController ; @protocol rightViewControllerDeldgate - ( void ) rightViewControllerDelegate : ( rightViewController * ) vc withIndexPatch : ( NSIndexPath * ) ptch ; - ( void ) rightViewControllerDelegateHeaderViewAppear : ( rightViewController * ) vc withIndexPatch : ( NSInteger ) section ; - ( void ) rightViewControllerDelegateHeaderViewdisappear : ( rightViewController * ) vc withIndexPatch : ( NSInteger ) section ; @end @interface rightViewController : UIViewController @property ( nonatomic , weak ) id delegate ; - ( void ) scrollToSelectIndexPath : ( NSIndexPath * ) path ; @endrightViewController.m
Objective-C 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 #import "rightViewController.h" @interface rightViewController ( ) @property ( nonatomic , strong ) UITableView *rightTableView ; @property ( nonatomic , strong ) NSArray *rightDataArr ; @property ( nonatomic , assign ) BOOL isScrollUp ; @property ( nonatomic , assign ) CGFloat lastOffsetY ; //滚动即将结束时scrollView的偏移量 @property ( nonatomic , assign ) BOOL ifScroll ; //当左边cell点击-> 右边滑动-> 右边滑动头消失头出现就会执行代理->代理方法再让左边滑动 给个boll值设置为NO就是为了 点击左边cell右边头出现或者消失 都不执行代理方法 左边也不会滑动. @end @implementation rightViewController - ( UITableView * ) rightTableView { if ( ! _rightTableView ) { _rightTableView = [ [ UITableView alloc ] initWithFrame :CGRectMake ( 0 , 0 , self . view . frame . size . width , self . view . frame . size . height ) style :UITableViewStylePlain ] ; _rightTableView . showsVerticalScrollIndicator = NO ; _rightTableView . delegate = self ; _rightTableView . dataSource = self ; [ _rightTableView registerClass : [ UITableViewCell class ] forCellReuseIdentifier : @"cell" ] ; [ _rightTableView registerClass : [ UITableViewHeaderFooterView class ] forHeaderFooterViewReuseIdentifier : @"headerView" ] ; } return _rightTableView ; } - ( NSArray * ) rightDataArr { if ( ! _rightDataArr ) { _rightDataArr = @ [ @ [ @"1橘子" , @"西瓜" , @"哈密瓜" , @"苹 果" , @"柚子" , @"香蕉" ] , @ [ @"2华为手机" , @"苹果5" , @"苹果5s" , @"苹果6" , @"苹果6+" , @"苹果6s" , @"苹果6s+" ] , @ [ @"3联想电脑" , @"华硕电脑" , @"MAC" ] , @ [ @"4鸡肉" , @"鸭肉" , @"牛肉" , @"猪肉" , @"羊肉" ] , @ [ @"5香菇" , @"蘑菇" , @"大头菜" , @"青椒" ] , @ [ @"6花生油" , @"大豆油" , @"葵花籽油" , @"芝 麻香油" ] , @ [ @"7花椒" , @"陈皮" , @"八角" ] , @ [ @"8橘子" , @"西瓜" , @"哈密瓜" , @"苹果" , @"柚子" , @"香蕉" ] , @ [ @"9华为手机" , @"苹果5" , @"苹果5s" , @"苹果6" , @"苹果6+" , @"苹果6s" , @"苹果6s+" ] , @ [ @"10联想电脑" , @"华硕电脑" , @"MAC" ] , @ [ @"11鸡肉" , @"鸭肉" , @"牛肉" , @"猪肉" , @"羊肉" ] , @ [ @"12香菇" , @"蘑菇" , @"大头菜" , @"青椒" ] , @ [ @"13花生油" , @"大豆油" , @"葵花籽油" , @"芝麻香油" ] , @ [ @"14花椒" , @"陈皮" , @"八角" ] , @ [ @"15橘子" , @"西瓜" , @"哈密瓜" , @"苹果" , @"柚子" , @"香蕉" ] , @ [ @"16华为手机" , @"苹果5" , @"苹果5s" , @"苹果6" , @"苹果6+" , @"苹果6s" , @"苹果6s+" ] , @ [ @"17联想电脑" , @"华硕电脑" , @"MAC" ] , @ [ @"18鸡肉" , @"鸭肉" , @"牛肉" , @"猪肉" , @"羊肉" ] , @ [ @"19香菇" , @"蘑菇" , @"大头菜" , @"青椒" ] , @ [ @"20花生油" , @"大豆油" , @"葵花籽油" , @"芝麻香油" ] , @ [ @"21花椒" , @"陈皮" , @"八角" ] ] ; } return _rightDataArr ; } - ( NSInteger ) numberOfSectionsInTableView : ( UITableView * ) tableView { return self . rightDataArr . count ; } - ( NSInteger ) tableView : ( UITableView * ) tableView numberOfRowsInSection : ( NSInteger ) section { NSArray *arr = self . rightDataArr [ section ] ; return arr . count ; } - ( UITableViewCell * ) tableView : ( UITableView * ) tableView cellForRowAtIndexPath : ( NSIndexPath * ) indexPath { UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier : @"cell" forIndexPath :indexPath ] ; NSArray * data = self . rightDataArr [ indexPath . section ] ; cell . textLabel . text = data [ indexPath . row ] ; return cell ; } - ( nullable UIView * ) tableView : ( UITableView * ) tableView viewForHeaderInSection : ( NSInteger ) section { UITableViewHeaderFooterView *headerView = [ tableView dequeueReusableHeaderFooterViewWithIdentifier : @"headerView" ] ; headerView . textLabel . text = [ NSString stringWithFormat : @"第%ld区" , section + 1 ] ; return headerView ; } - ( CGFloat ) tableView : ( UITableView * ) tableView heightForHeaderInSection : ( NSInteger ) section { return 30 ; } - ( void ) viewDidLoad { [ super viewDidLoad ] ; self . view . frame = CGRectMake ( 120 , 64 , self . view . frame . size . width - 120 , self . view . frame . size . height - 64 ) ; self . view . backgroundColor = [ UIColor redColor ] ; [ self . view addSubview :self . rightTableView ] ; } - ( void ) tableView : ( UITableView * ) tableView didSelectRowAtIndexPath : ( NSIndexPath * ) indexPath { if ( _delegate && [ _delegate respondsToSelector : @selector ( rightViewControllerDelegate :withIndexPatch : ) ] ) { [ _delegate rightViewControllerDelegate :self withIndexPatch :indexPath ] ; } } //头将出现 - ( void ) tableView : ( UITableView * ) tableView willDisplayHeaderView : ( UIView * ) view forSection : ( NSInteger ) section { if ( _delegate && [ self . delegate respondsToSelector : @selector ( rightViewControllerDelegateHeaderViewAppear :withIndexPatch : ) ] && ! _isScrollUp && _ifScroll ) { [ _delegate rightViewControllerDelegateHeaderViewAppear :self withIndexPatch :section ] ; } } //头讲消失 - ( void ) tableView : ( UITableView * ) tableView didEndDisplayingHeaderView : ( UIView * ) view forSection : ( NSInteger ) section { if ( _delegate && [ self . delegate respondsToSelector : @selector ( rightViewControllerDelegateHeaderViewdisappear :withIndexPatch : ) ] && _isScrollUp && _ifScroll ) { //上滑才执行 [ _delegate rightViewControllerDelegateHeaderViewdisappear :self withIndexPatch :section + 1 ] ; } } //主要判断向上滑动还是向下滑动 - ( void ) scrollViewDidScroll : ( UIScrollView * ) scrollView { _isScrollUp = _lastOffsetY