实现 UISegmentControl 与 UIScrollView的上下级联(分别在相应的方法中加入级联代码)
实现 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的上下级联(分别在相应的方法中加入级联代码)的更多相关文章
- [原创]关于Hibernate中的级联操作以及懒加载
Hibernate: 级联操作 一.简单的介绍 cascade和inverse (Employee – Department) Casade用来说明当对主对象进行某种操作时是否对其关联的从对象也作类似 ...
- 利用opencv中的级联分类器进行人脸检測-opencv学习(1)
OpenCV支持的目标检測的方法是利用样本的Haar特征进行的分类器训练,得到的级联boosted分类器(Cascade Classification).注意,新版本号的C++接口除了Haar特征以外 ...
- EBS採购模块中的级联接收和级联接收事务
EBS採购模块中的级联接收和级联接收事务 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习.请注明出处:否则请与本人联系.违者必究) 级联接收和级联接收事务 级联功能对来自于同一个供应商 ...
- 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
原文链接:https://www.entityframeworktutorial.net/code-first/cascade-delete-in-code-first.aspx EF 6 Code- ...
- (原创)Hibernate 使用过程中(尤其是多对多关联中的级联保存和级联删除)的注意事项(基于项目的总结)
一.先上知识点: 1.hibernate多对多关联关系中最重要的参数是(基于配置文件xxx.hbm.xml文件形式): 1):inverse属性,如果设置inverse=“true”就代表让对方参与维 ...
- Mysql学习笔记(八)由触发器回顾外键约束中的级联选项
近些天都没有写博客.在学习mysql的知识,通过学习和练习,也熟悉了mysql的函数.触发器.视图和存储过程.并且在实际的开发过程中也应用了一小部分.效果还是十分理想的. 今天晚上在学习触发器模仿in ...
- 前台JS(Jquery)调用后台方法 无刷新级联菜单示例
前台用AJAX直接调用后台方法,老有人发帖提问,没事做个示例 下面是做的一个前台用JQUERY,AJAX调用后台方法做的无刷新级联菜单 http://www.dtan.so CasMenu.aspx页 ...
- Devexpress GridControl中combobox级联显示 z
http://minmin86121.blog.163.com/blog/static/4968115720143163533356/ 在 使用GridControl时,可能会有需求要求某2列显示co ...
- thinkPHP中省市级联下拉列表
公共函数放置位置common文件夹下common.php文件(此段代码也可放置在要使用的控制器中) 封装的下拉列表函数代码: /** * 根据列表拼装成一个下拉列表 ADD BY CK * @para ...
随机推荐
- COJ968 WZJ的数据结构(负三十二)
WZJ的数据结构(负三十二) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,边上均有权值,每个点上有 ...
- SQL 标量函数-----日期函数datediff()、 day() 、month()、year()
select day(createtime) from life_unite_product --取时间字段的天值 select month(createtime) from life_uni ...
- JAVA操作COOKIE
JAVA操作COOKIE 1.设置Cookie Cookie cookie = new Cookie("key", "value"); cookie.setMa ...
- 关于UIWebView的总结
关于UIWebView的总结 前言 今天参加了 Adobe 和 CSDN 组织的一个关于 PhoneGap 的开发讲座 ,而 PhoneGap 在 iOS 设备上的实现就是通过 UIWebView 控 ...
- CCAction
之前介绍CCNode的时候说过,动作是指在特定时间内完成移动.缩放.旋转等操作的行为,节点可以通过运行动作来实现动画效果,这里的动作就是指CCAction对象,它有很多的子类,每个子类都封装了不同的动 ...
- 【C语言】03-第一个C程序代码分析
前面我们已经创建了一个C程序,接下来分析一下里面的代码. 项目结构如下: 一.代码分析 打开项目中的main.c文件(C程序的源文件拓展名为.c),可以发现它是第一个C程序中的唯一一个源文件,代码如下 ...
- 新浪微博API开放平台进行程序开发第一步(java)
申请开发者权限步骤: 1.登录sina微博,点击“应用” 2.点击“微博开发平台 我也要做开发者” 3.点击“我的应用”,填写“开发者信息” 4.点击“创建应用”,就是你将要开发的微博应用程序,可以是 ...
- [转]动态调用webservice时 ServiceDescriptionImporter类在vs2010无法引用的解决方法
本文转自:http://blog.csdn.net/limlimlim/article/details/8647038 [导读]ServiceDescriptionImporter是创建Web Ser ...
- gcc/g++链接时.o文件及库的顺序问题
折腾gcc/g++链接时.o文件及库的顺序问题 链接静态库的顺序问题 GCC 编译使用动态链接库和静态链接库--及先后顺序----及环境变量设置总结
- PHP mkdir 方法 创建 0777 权限的目录问题
php 中使用 mkdir() 方法创建 0777 权限的目录: $path = './Logs/secondCheck/';if(!is_dir($path)){ mkdir($path, 0777 ...