实现 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 ...
随机推荐
- POST模拟百度登录和自动发帖
这里用HttpClient发包模拟百度登录和发帖,验证码部分采用机器下载人工识别. 登陆百度的原理:1. 访问https://passport.baidu.com/v2/api/?getapi& ...
- RN组件之ListView
/** * Created by DaGuo on 2016/4/7. */ 'use strict' import React,{ Component, View, Text, ListView, ...
- CSS中a标签样式的“爱恨”原则
CSS为一些特殊效果准备了特定的工具,我们称之为“伪类”.其中有几项是我们经常用到的,下面我们就详细介绍一下经常用于定义链接样式的四个伪类,它们分别是: 1 :link 2 :visited 3 :h ...
- 在iOS开发中使用FMDB
在iOS开发中使用FMDB 前言 SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK 很早就支持了 SQLite,在使用时,只需 ...
- 李洪强-C语言7-C语言运算符
C语言运算符 一.算术运算 C语言一共有34种运算符,包括常见的加减乘除运算. ①. 加法:+ 还可以表示正号 ②. 减法:- 还可以表示负号 ③. 乘法:* 非数学意义上的X ④. 除法:/ 注意 ...
- 在Eclipse中配置Tomcat 创建和运行Servlet/JSP
在Eclipse中配置Tomcat 创建和运行Servlet/JSP 步骤一:在Eclipse中配置Tomcat(注意下载Eclipse IDE for Java EE Developers) (1) ...
- hdu Train Problem I
这道题是道简单的栈模拟题,只要按照真实情况用栈进行模拟即可: #include<stdio.h> #include<string.h> #include<stack> ...
- 通过IP的方式建立PSSession
Import-Module PoshWSUS Connect-PoshWSUSServer -WsusServer cnhzsrv09 Get-PoshWSUSClient | Select-Obje ...
- 【液晶模块系列基础视频】1.1.iHMI43模块介绍
[液晶模块系列基础视频]1.1.iHMI43模块介绍(上) [液晶模块系列基础视频]1.1.iHMI43模块介绍(下) ============================== 技术论坛:http ...
- 小组项目beta发布的评价
这次最看好飞天小女警组,相比上次他们的界面漂亮了很多,功能也相对完善,他们的礼物挑选系统非常有创意.如果去网上爬更多的数据,这个项目会更完美. 新蜂团队的俄罗斯方块游戏新增加了显示下一个方块以及游戏积 ...