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 運行效果


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容