iOStableview联动

首先你要创建两个tableview 然后初始化一下,最好是懒加载 

 左边的 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.backgroundColor = [UIColor redColor];

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

}

return _leftTableView;

}

 右边的 tableView

- (UITableView *)rightTableView {

if (!_rightTableView) {

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

[self.view addSubview:tableView];

_rightTableView = tableView;

tableView.dataSource = self;

tableView.delegate = self;

tableView.backgroundColor = [UIColor cyanColor];

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

}

return _rightTableView;

}

然后先设置cell

判断一下如果是左边的tableview 就显示相应的标题的数量 先判断有多少个cell

- (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;

}

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

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,113评论 3 38
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 2,921评论 2 10
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,477评论 9 5
  • 说来有些好笑,我爱吸书。 当开学之际,从学校领回一打新书时,我总会小心翼翼的摊开一面,把鼻子凑上去闻那...
    武汉全智贤阅读 524评论 1 2
  • 彤姑娘自从跟彤妈学了几句有文化的诗句之后,就整天一安静下来就念叨那几句诗,但是总是有几句会被她不经意间篡改掉,比如...
    寒峰云阅读 236评论 0 1