iOS 两个tableView联动效果的实现

1在storyboard中放好两个tableView的布局


2 代码部分

#import "TwoTablesViewController.h"

@interface TwoTablesViewController ()

@property (weak, nonatomic) IBOutlet UITableView *leftTableView;

@property (weak, nonatomic) IBOutlet UITableView *rightTableView;

@end

@implementation TwoTablesViewController {

NSArray *_leftArray;

NSArray *_rightArray;

}

- (void)viewDidLoad {

[super viewDidLoad];

_leftArray = [[NSArray alloc] initWithObjects:@"第一类",@"第二类",@"第三类",@"第四类",@"第五类",@"第六类",@"第七类",@"第八类", nil];

_rightArray = [[NSArray alloc] initWithObjects:@"一",@"二",@"三",@"四",@"五",@"六", nil];

[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if (tableView == _rightTableView) {

return [_leftArray objectAtIndex:section];

}

return nil;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

if (tableView == _rightTableView) {

return [_leftArray count];

}

return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (tableView == self.leftTableView) {

return _leftArray.count;

}

else if (tableView == self.rightTableView) {

return _rightArray.count;

}

return 1;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];

if (tableView == self.leftTableView) {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell"];

cell.textLabel.text = [_leftArray objectAtIndex:indexPath.row];

return cell;

}

else  {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightCell"];

cell.textLabel.text = [_rightArray objectAtIndex:indexPath.row];;

return cell;

}

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (tableView == _leftTableView) {

//        [_rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:YES];

[_rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:YES scrollPosition:UITableViewScrollPositionTop];

}

else {

[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];

}

}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

NSLog(@"33333333");

if (scrollView == _rightTableView) {

NSIndexPath *indexPath = [[_rightTableView indexPathsForVisibleRows ] objectAtIndex:0];

[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];

}

}

//滑动停止时执行

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

NSLog(@"33333333");

if (scrollView == _rightTableView) {

NSIndexPath *indexPath = [[_rightTableView indexPathsForVisibleRows ] objectAtIndex:0];

[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];

}

}

@end

3 运行效果


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容