美文网首页
iOStableview联动

iOStableview联动

作者: 路边的风景呢 | 来源:发表于2017-05-25 14:35 被阅读22次

首先你要创建两个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;

}

相关文章

网友评论

      本文标题:iOStableview联动

      本文链接:https://www.haomeiwen.com/subject/vigaxxtx.html