列表联动数字

导入

#define leftTableWidth [UIScreen mainScreen].bounds.size.width * 0.3

#define rightTableWidth [UIScreen mainScreen].bounds.size.width * 0.7

#define ScreenWidth  [UIScreen mainScreen].bounds.size.width

#define ScreenHeight [UIScreen mainScreen].bounds.size.height

#define leftCellIdentifier @"leftCellIdentifier"

#define rightCellIdentifier @"rightCellIdentifier"


<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *leftTableView;

@property (nonatomic, weak) UITableView *rightTableView;


=============括号内==

self.view.backgroundColor = [UIColor whiteColor];

//分类

self.title = @"分类";

[self.view addSubview:self.leftTableView];

[self.view addSubview:self.rightTableView];

self.view.backgroundColor = [UIColor whiteColor];

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:(UIBarButtonItemStyleDone) target:self action:nil];

--=======括号外=====

//#pragma mark - tableView 数据源代理方法 -

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

if (tableView == self.leftTableView) return 40;

return 8;

}

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

if (tableView == self.leftTableView) return 1;

return 40;

}

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

UITableViewCell *cell;

// 左边的 view

if (tableView == self.leftTableView) {

cell = [tableView dequeueReusableCellWithIdentifier:leftCellIdentifier forIndexPath:indexPath];

cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];

// 右边的 view

} else {

cell = [tableView dequeueReusableCellWithIdentifier:rightCellIdentifier forIndexPath:indexPath];

cell.textLabel.text = [NSString stringWithFormat:@"第%ld组-第%ld行", indexPath.section, indexPath.row];

}

return cell;

}

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

if (tableView == self.rightTableView) return [NSString stringWithFormat:@"第 %ld 组", section];

return nil;

}

#pragma mark - UITableViewDelegate 代理方法 -

//MARK: - 一个方法就能搞定 右边滑动时跟左边的联动

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

// 如果是 左侧的 tableView 直接return

if (scrollView == self.leftTableView) return;

// 取出显示在 视图 且最靠上 的 cell 的 indexPath

NSIndexPath *topHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject];

// 左侧 talbelView 移动的 indexPath

NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0];

// 移动 左侧 tableView 到 指定 indexPath 居中显示

[self.leftTableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

}

//MARK: - 点击 cell 的代理方法

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

// 选中 左侧 的 tableView

if (tableView == self.leftTableView) {

NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];

// 将右侧 tableView 移动到指定位置

[self.rightTableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

// 取消选中效果

[self.rightTableView deselectRowAtIndexPath:moveToIndexPath animated:YES];

}

}

#pragma mark - 懒加载 tableView -

// MARK: - 左边的 tableView

- (UITableView *)leftTableView {

if (!_leftTableView) {

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, leftTableWidth, ScreenHeight)];

[self.view addSubview:tableView];

_leftTableView = tableView;

tableView.dataSource = self;

tableView.delegate = self;

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:leftCellIdentifier];

tableView.backgroundColor = [UIColor redColor];

tableView.tableFooterView = [[UIView alloc] init];

}

return _leftTableView;

}

// MARK: - 右边的 tableView

- (UITableView *)rightTableView {

if (!_rightTableView) {

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(leftTableWidth, 64, rightTableWidth, ScreenHeight)];

[self.view addSubview:tableView];

_rightTableView = tableView;

tableView.dataSource = self;

tableView.delegate = self;

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:rightCellIdentifier];

tableView.backgroundColor = [UIColor cyanColor];

tableView.tableFooterView = [[UIView alloc] init];

}

return _rightTableView;

}

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

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,086评论 3 38
  • 1.nav1.navigationBar.barStyle=UIBarStyleBlack; //改变导航栏背景颜...
    SadMine阅读 1,615评论 1 4
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,532评论 1 14
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 2,915评论 2 10
  • 几忆花下坐吹箫,寂寂小亭轻看遥。 此夜良辰非昨夜,斜斜月华照断桥, 画桥霜夜冻同色,风舞冰花自作声。 月夜多是离人...
    一个电压表阅读 394评论 0 2