实现 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. 大白话strom——问题收集(持续更新ing)

    本文导读: 1.基于storm的应用 2.storm的单点故障解决 3.strom与算法的结合学习4.杂记——常见问题的解答5.http://www.blogchong.com/catalog.asp ...

  2. 关于 Apple Metal API 的一些想法

    在看完 Metal 的开发文档后,除了官方所宣称的一些优点外(比如说更容易理解和使用的 API,更直接和精细的硬件控制,减少 GPU 使用过程中的 CPU 额外开销等等),从我有限的 GLES 开发经 ...

  3. 一人一python挑战题解

    题目id: 1 just print a+b give you two var a and b, print the value of a+b, just do it!! print a+b 题目id ...

  4. 关于如何用php 获取当前脚本的url

    关于用php 获取当前脚本的url很多朋友会说很简单,但是要获取很详细的就要经过多次判断哦. $PHP_TIME = time();$PHP_SELF = isset($_SERVER['PHP_SE ...

  5. Hadoop配置文件解析

    Hadoop源码解析 2 --- Hadoop配置文件解析 1 Hadoop Configuration简介    Hadoop没有使用java.util.Properties管理配置文件, 也没有使 ...

  6. 【iCore2双核心板】SRAM 读写实验(基于Verilog语言)

    _____________________________________ 深入交流QQ群: A: 204255896(1000人超级群,可加入) B: 165201798(500人超级群,满员) C ...

  7. ORA-12518: TNS: 监听程序无法分发客户机连接

    在团队成员增多时,经常出现“无法分发客户端连接”等问题.在网上搜索一番后,最终解决了该问题,现将解决方案总结如下,以供参考和以后备用. 原因:团队成员增多,原有数据库设置不够用,导致连接plsql和启 ...

  8. java int转integer方法

    由于AutoBoxing的存在,以下代码在JDK1.5的环境下可以编译通过并运行. int i = 0; Integer wrapperi = i; 还有其他方法? JDK1.5为Integer增加了 ...

  9. Mac OS X 中安装JDK 7

    通过Mac系统的更新安装Java的版本均为JDK 6的版本,如果想要在Mac上安装JDK 7,就需要到Oracle的网站上去下载相应的安装包. 下面为详细教程: 最新版本为JDK8,目前需求JDK7够 ...

  10. Oracle 语句常见错误

    Merge into的注意点之ORA-30926:无法在源表中获得一组稳定的行? merge into 的内部处理是将table_source 的每一条记录和table_target的每一条记录对比匹 ...