// 创建两个tablview
#import "ViewController.h"
#define SCREEN_WIDTH self.view.frame.size.width
#define SCREEN_HEIGHT self.view.frame.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSArray *Proarr;
NSArray *cityAtt;
}
@property (nonatomic,strong)UITableView *LeftTable;
@property (nonatomic,strong)UITableView *RightTable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Proarr =@[@"河北",@"河南",@"吉林",@"山东",@"山西"];
cityAtt=@[@[@"1",@"2",@"2",@"2",@"2",@"2",@"2"],@[@"2",@"2"],@[@"3",@"3",@"3"],@[@"4",@"4",@"4",@"4"],@[@"5",@"5",@"5"@"5",@"5"]];
[self.view addSubview:self.LeftTable];
[self.view addSubview:self.RightTable];
}
-(UITableView *)LeftTable
{
if (!_LeftTable)
{
_LeftTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH-100 , SCREEN_HEIGHT) style:UITableViewStyleGrouped];
_LeftTable.tag = 100;
_LeftTable.delegate = self;
_LeftTable.dataSource = self;
}
return _LeftTable;
}
-(UITableView *)RightTable
{
if (!_RightTable) {
_RightTable = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100, (SCREEN_HEIGHT-150)/2,150 , 30*5) style:UITableViewStylePlain];
_RightTable.rowHeight = 30;
_RightTable.tag = 110;
_RightTable.delegate = self;
_RightTable.dataSource = self;
}
return _RightTable;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if (tableView.tag == 100) {
return Proarr.count;
}else
{
return 1;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == 100)
{
NSArray *arr = cityAtt[section];
return arr.count;
}
else
{
return Proarr.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"];
}
if (tableView.tag == 100)
{
cell.textLabel.text = cityAtt[indexPath.section][indexPath.row];
}else
{
cell.textLabel.text = Proarr[indexPath.row];
}
return cell;
}
// 点击一个 另一个滚动到响应位置
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 110)
{
NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
[self.LeftTable scrollToRowAtIndexPath:moveToIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
// 滑动一个使另一个到具体位置
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.LeftTable)
{
NSIndexPath *topHeaderViewIndexpath = [[self.LeftTable indexPathsForVisibleRows] firstObject];
NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0];
[self.RightTable selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
}
// 页眉
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView.tag == 100)
{
return Proarr[section];
}else
{
return 0;
}
}
// 页脚
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
NSArray *arr = cityAtt[section];
NSString *str = [NSString stringWithFormat:@"共有%lu个城市",(unsigned long)arr.count];
if (tableView.tag == 100) {
return str;
}
else
{
return 0;
}
}