实现 UISegmentControl 与 UIScrollView的上下级联,需要在
[segmentCtr addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];方法中加入// (级联)根据选中的第几段来计算scrollView的滚动位置(contentOffSet)
和
-(void)scrollViewDidScroll:(UIScrollView *)scrollView;方法中分别加入"@@级联@@"的代码计算// (级联)根据scrollView的滚动位置决定显示第几段
//
// ViewController.m
// ScrollPlayerDemo
//
// Created by diesel on 16/2/21.
// Copyright © 2016年 JingFang. All rights reserved.
//
#import "ViewController.h"
#define ScreenWidth self.view.frame.size.width
#define ScreenHeight self.view.frame.size.height
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UIScrollView *myScrollView;
UITableView *tableView0;
NSArray *dataSource0;
// UITableView *tableView1;
NSArray *dataSource1;
//
// UITableView *tableView2;
NSArray *dataSource2;
UISegmentedControl *segmentCtr;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
}
- (void)createUI{
segmentCtr = [[UISegmentedControl alloc]initWithItems:@[@"昨天",@"今天",@"明天"]];
segmentCtr.frame = CGRectMake(100, 20, 200, 40);
segmentCtr.selectedSegmentIndex = 0;
[segmentCtr addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentCtr];
dataSource0 = @[@"1",@"2",@"3",@"4",@"1",@"2",@"3",@"4",@"1",@"2",@"3",@"4"];
dataSource1 = @[@"11",@"22",@"33",@"44"];
dataSource2 = @[@"1111",@"211",@"311",@"4",@"1",@"2",@"3",@"4"];
myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 80, ScreenWidth , ScreenHeight)];
myScrollView.backgroundColor = [UIColor yellowColor];
myScrollView.pagingEnabled = YES;
myScrollView.delegate = self;
myScrollView.showsHorizontalScrollIndicator = NO;
myScrollView.contentSize = CGSizeMake(ScreenWidth * 3, ScreenHeight);
[self.view addSubview:myScrollView];
for (int i = 0; i<3; i++) {
tableView0 = [[UITableView alloc]init];
tableView0.frame = CGRectMake(0+ScreenWidth*i, 0, ScreenWidth, ScreenHeight-160);
tableView0.tag = i+1;
tableView0.dataSource = self;
tableView0.delegate = self;
[myScrollView addSubview:tableView0];
}
}
-(void)segmentedControlValueChanged:(UISegmentedControl *)segment{
//级联
CGFloat offsetX = segment.selectedSegmentIndex * ScreenWidth;
CGPoint offset = CGPointMake(offsetX, 0);
[myScrollView setContentOffset:offset animated:YES];
// UITableView *tableView = [myScrollView viewWithTag:segmentCtr.selectedSegmentIndex + 1];
// [tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (segmentCtr.selectedSegmentIndex == 0) {
return dataSource0.count;
}else if(segmentCtr.selectedSegmentIndex == 1){
return dataSource1.count;
}
return dataSource2.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *string = @"string";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
}
if (segmentCtr.selectedSegmentIndex == 0) {
cell.textLabel.text = dataSource0[indexPath.row];
}else if(segmentCtr.selectedSegmentIndex == 1){
cell.textLabel.text = dataSource1[indexPath.row];
}else{
cell.textLabel.text = dataSource2[indexPath.row];
}
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// (级联)根据scrollView的滚动位置决定显示 第几段
CGFloat scrollW = scrollView.frame.size.width;
int page = (scrollView.contentOffset.x + scrollW * 0.5) / scrollW;
segmentCtr.selectedSegmentIndex = page;
//当换页时,重新加载数据源(注意:tag值不能从0开始)
UITableView *tableView = [self.view viewWithTag:segmentCtr.selectedSegmentIndex + 1];
[tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end