实现 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

实现 UISegmentControl 与 UIScrollView的上下级联(分别在相应的方法中加入级联代码)的更多相关文章

  1. [原创]关于Hibernate中的级联操作以及懒加载

    Hibernate: 级联操作 一.简单的介绍 cascade和inverse (Employee – Department) Casade用来说明当对主对象进行某种操作时是否对其关联的从对象也作类似 ...

  2. 利用opencv中的级联分类器进行人脸检測-opencv学习(1)

    OpenCV支持的目标检測的方法是利用样本的Haar特征进行的分类器训练,得到的级联boosted分类器(Cascade Classification).注意,新版本号的C++接口除了Haar特征以外 ...

  3. EBS採购模块中的级联接收和级联接收事务

    EBS採购模块中的级联接收和级联接收事务 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习.请注明出处:否则请与本人联系.违者必究) 级联接收和级联接收事务 级联功能对来自于同一个供应商 ...

  4. 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/cascade-delete-in-code-first.aspx EF 6 Code- ...

  5. (原创)Hibernate 使用过程中(尤其是多对多关联中的级联保存和级联删除)的注意事项(基于项目的总结)

    一.先上知识点: 1.hibernate多对多关联关系中最重要的参数是(基于配置文件xxx.hbm.xml文件形式): 1):inverse属性,如果设置inverse=“true”就代表让对方参与维 ...

  6. Mysql学习笔记(八)由触发器回顾外键约束中的级联选项

    近些天都没有写博客.在学习mysql的知识,通过学习和练习,也熟悉了mysql的函数.触发器.视图和存储过程.并且在实际的开发过程中也应用了一小部分.效果还是十分理想的. 今天晚上在学习触发器模仿in ...

  7. 前台JS(Jquery)调用后台方法 无刷新级联菜单示例

    前台用AJAX直接调用后台方法,老有人发帖提问,没事做个示例 下面是做的一个前台用JQUERY,AJAX调用后台方法做的无刷新级联菜单 http://www.dtan.so CasMenu.aspx页 ...

  8. Devexpress GridControl中combobox级联显示 z

    http://minmin86121.blog.163.com/blog/static/4968115720143163533356/ 在 使用GridControl时,可能会有需求要求某2列显示co ...

  9. thinkPHP中省市级联下拉列表

    公共函数放置位置common文件夹下common.php文件(此段代码也可放置在要使用的控制器中) 封装的下拉列表函数代码: /** * 根据列表拼装成一个下拉列表 ADD BY CK * @para ...

随机推荐

  1. HTML head 头标签

    HTML head 头部分的标签.元素有很多,涉及到浏览器对网页的渲染,SEO 等等,而各个浏览器内核以及各个国内浏览器厂商都有些自己的标签元素,这就造成了很多差异性.移动互联网时代,head 头部结 ...

  2. BZOJ1196: [HNOI2006]公路修建问题

    Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织 ...

  3. java中的Integer的toBinaryString()方法

    在一次面试的过程中,遇到过这样的题目,题目的大概意思是:让写出Integer类中的toBinaryString()方法 也就是说,把Integer转换为Binary的过程写出来 但是我蒙的,在查了JD ...

  4. linux 快捷键

    截图: 打印: 全屏截图 alt+打印:窗口截图 shift+打印:区域截图 ctrl+打印:截图到剪贴板 显示桌面: ctrl+super+d 最大化最小化窗口 ctrl+alt+up/down 转 ...

  5. ZXing二维码的生成和解析

    Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法, 现在我简单介绍一下使用Java利用Zxing生成与解析二维码 注意: 二维码的生成需要借助辅助类( ...

  6. 记在thinkPHP中一个创建模型的小错误

    在创建好模型以后,访问说没有该方法,如图 看代码 class ManagerModel { //put your code here function checkDenglu($name,$pwd){ ...

  7. php 的curl 模拟登陆

    做一个类似这样的web 应用. 1,解决掉验证码 其实这是正方的一个小bug,当我们进入登陆界面时,浏览器会去请求服务器,服务器会生成一个验证码图片.如果我们不去请求这个图片,那么正方后台也不会生成相 ...

  8. discuz怎么根据连接知道调用的是什么模板页面

    其实不怎么难,基本都可以看出discuz是怎么样调用模板页面的 这个是论坛的帖子的列表页,看到url就可以看出是forum目录下的forumdisplay这个模板,forumdisplay.html这 ...

  9. Solr学习笔记之1、环境搭建

    Solr学习笔记之1.环境搭建 一.下载相关安装包 1.JDK 2.Tomcat 3.Solr 此文所用软件包版本如下: 操作系统:Win7 64位 JDK:jdk-7u25-windows-i586 ...

  10. Grand Theft Auto V 图形研究(2)

    原文链接 http://www.adriancourreges.com/blog/2015/11/02/gta-v-graphics-study-part-2/   Level of Detail 如 ...