美文网首页
表格连动效果

表格连动效果

作者: 小緈福 | 来源:发表于2018-06-25 10:07 被阅读0次

摘要:两个tableview的联动,滑动左侧tableview,右侧tableview跟着滑动其实实现起来比较简单,只是需要搞清楚他们之间的区别和联系,还有就是调用一个-(void)tableView:(UITableView*)tableViewwillDisplayHeaderView:(UIView*)viewforSection:(NSInteger)section这个方法,从而实现左右两个tableview的联动直接上代码@implementationViewContro

两个tableview的联动,滑动左侧tableview,右侧tableview跟着滑动

其实实现起来比较简单,只是需要搞清楚他们之间的区别和联系,还有就是调用一个

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

这个方法,从而实现左右两个tableview的联动

直接上代码

@implementation ViewController

{

UITableView *_rightTableView;

UITableView *_leftTableView;

NSArray *_leftTableSource;

NSArray *_rightTableSource;

}

初始化数据源

- (void)viewDidLoad {

[superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

_leftTableSource=@[@"11",@"22",@"33",@"44",@"55",@"66"];

_rightTableSource=@[@{@"header":@"11",@"title":@[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff"]},

@{@"header":@"22",@"title":@[@"gg",@"mm",@"nn",@"oo",@"pp",@"qq"]},

@{@"header":@"33",@"title":@[@"rr",@"ss",@"jj",@"xx",@"yy",@"zz"]},

@{@"header":@"44",@"title":@[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff"]},

@{@"header":@"55",@"title":@[@"gg",@"mm",@"nn",@"oo",@"pp",@"qq"]},

@{@"header":@"66",@"title":@[@"rr",@"ss",@"jj",@"xx",@"yy",@"zz"]}];

[selfsetupSomeParamars];

}

创建两个tableview

- (void)setupSomeParamars

{

_rightTableView= [[UITableViewalloc] initWithFrame:CGRectMake(100,0,self.view.frame.size.width-100,self.view.frame.size.height)style:UITableViewStyleGrouped];

_rightTableView.dataSource=self;

_rightTableView.delegate=self;

[self.viewaddSubview:_rightTableView];

_leftTableView= [[UITableViewalloc] initWithFrame:CGRectMake(0,0,100,self.view.frame.size.height)style:UITableViewStyleGrouped];

_leftTableView.dataSource=self;

_leftTableView.delegate=self;

[self.viewaddSubview:_leftTableView];

}

设置cell的显示

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

{

staticNSString *reuseIdentifer =@"cell";

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:reuseIdentifer];

if(!cell){

cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reuseIdentifer];

    }

if(tableView ==_rightTableView){

cell.textLabel.text = [_rightTableSource[indexPath.section]objectForKey:@"title"][indexPath.row];

}elseif (tableView ==_leftTableView){

cell.textLabel.text =_leftTableSource[indexPath.row];

    }

return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

if (tableView ==_rightTableView) {

return50;

}else{

return50;

    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

if (tableView ==_rightTableView) {

return_rightTableSource.count;

}else{

return1;

    }

}

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

{

if (tableView ==_leftTableView) {

return_leftTableSource.count;

}else{

return [[_rightTableSource[section]objectForKey:@"title"]count];

    }

}

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

{

if (tableView ==_rightTableView) {

return [_rightTableSource[section]objectForKey:@"header"];

}else{

returnnil;

    }

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

if(tableView ==_rightTableView){

UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width-100,40)];

label.backgroundColor= [UIColorcyanColor];

label.text = [_rightTableSource[section]objectForKey:@"header"];

label.textColor = [UIColorredColor];

return label;

}else{

returnnil;

    }

}

联动效果在于这里

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

{

if(tableView ==_rightTableView){

[_leftTableViewselectRowAtIndexPath:[NSIndexPathindexPathForItem:section inSection:0]animated:YESscrollPosition:UITableViewScrollPositionNone];

    }

}

相关文章

网友评论

      本文标题:表格连动效果

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