实现 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 ...
随机推荐
- js对象数组按属性快速排序
前一篇<关于selector性能比赛>中提到,目测觉得在$("div,p,a")这样有逗号时,sizzle耗时异常(600多个元素,花了200ms),说是它可能没有优化 ...
- [Cocos2d-x For WP8]点击移动精灵
点击移动是游戏里面常用的操作,那么在Cocos2d-x里面可以通过setTouchEnabled(true)方法来设置接收屏幕的点击事件,然后添加ccTouchesEnded(CCSet* touch ...
- X.509证书_生成X.509协议的证书
用法:1. 用NOTE打开,修改按实际情况脚本中的(1)~ (6)处参数2. 找一台含JVM环境的WIN机器3. 双击执行后,会生成一对密钥4. 请确保当前使用的JDK版本为6.0!!! @echo ...
- Easyui的datagrid结合hibernate实现数据分页
最近在学习easyui的使用,在学到datagrid的时候遇到了一些问题,终于抽点时间整理了一下,分享出来,请各位前辈高手多多指教! 1.先来看看效果,二话不说,上图直观! 2.easyui的data ...
- 新浪微博iOS客户端架构与优化之路
新浪微博iOS客户端架构与优化之路 随着Facebook.Twitter.微博的崛起,向UGC.PGC.OGC,自媒体提供平台的内 容消费型App逐渐形成了独特的客户端架构模式.与电商和通讯工具类 ...
- JAVA String.format 方法使用介绍
1.对整数进行格式化:%[index$][标识][最小宽度]转换方式 我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解, ...
- jquery CDN(内容分发网络)使用
jquery CDN 给开发者提供一种捷径,即不下载jquary 就通过CDN能使用各个版本的jquery. 使用方法很简单,就是在HTML 文档中引用相关版本的jquery. 例如:我用百度的CDN ...
- 2016.07.04,英语,《Vocabulary Builder》Unit 23
text comes from a Latin verb that means 'to weave'. textile: ['tekstaɪl] adj. 纺织的 n. 纺织品; texture: [ ...
- DS实验题 sights
算法与数据结构实验题 6.3 sights ★实验任务 美丽的小风姑娘打算去旅游散心,她走进了一座山,发现这座山有 n 个景点, 由于山路难修,所以施工队只修了最少条的路,来保证 n 个景点联通,娇弱 ...
- PHP 设计模式 笔记与总结(4)PHP 链式操作的实现
PHP 链式操作的实现 $db->where()->limit()->order(); 在 Common 下创建 Database.php. 链式操作最核心的地方在于:在方法的最后 ...