collectionView 和 tableView的嵌套使用

#import "ViewController.h"
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define WIDTH [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView* backvView;
@property(nonatomic,strong)UIScrollView* secondScrollView;
@property(nonatomic,strong)UICollectionView* collectionView;
@property(nonatomic,strong)UITableView * tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加ui
[self AddUI];
//添加集合视图
[self collectionViewMethods];
//添加单元格
[self tableViewMethods];
}
-(void)AddUI
{
_backvView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-40)];
_backvView.backgroundColor=[UIColor cyanColor];
[self.view addSubview:_backvView];
UIImageView* fristImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 520.0/2208.0*HEIGHT)];
fristImageView.backgroundColor = [UIColor yellowColor];
[_backvView addSubview:fristImageView];
_secondScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 520.0/2208.0*HEIGHT, WIDTH, 640.0/2208.0*HEIGHT)];
_secondScrollView.backgroundColor=[UIColor blueColor];
[_backvView addSubview:_secondScrollView];
UIView * thirdView=[[UIView alloc]initWithFrame:CGRectMake(0, 1160.0/2208.0*HEIGHT, WIDTH,30.0/2208.0*HEIGHT)];
thirdView.backgroundColor=[UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
[_backvView addSubview:thirdView];
UIView* fourthView=[[UIView alloc]initWithFrame:CGRectMake(0, 1190.0/2208.0*HEIGHT, WIDTH, 830.0/2208.0*HEIGHT)];
fourthView.backgroundColor=[UIColor greenColor];
[_backvView addSubview:fourthView];
UIView* fifthView=[[UIView alloc]initWithFrame:CGRectMake(0, 2010.0/2208.0*HEIGHT, WIDTH, 45.0/2208.0*HEIGHT)];
fifthView.backgroundColor=[UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
[_backvView addSubview:fifthView];
}
-(void)tableViewMethods
{
_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
_tableView.delegate=self;
_tableView.dataSource=self;
[self.view addSubview:_tableView];
_tableView.tableHeaderView=_backvView;
}
-(void)collectionViewMethods
{
//初始化集合视图对象
UICollectionViewFlowLayout* layout=[[UICollectionViewFlowLayout alloc]init];
//设置集合视图滚动方向
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//设置item的大小
[layout setItemSize:CGSizeMake(150.0/1242.0*WIDTH, 150.0/2208.0*HEIGHT)];
//设置集合视图的范围 距离边距 依次是上左下右
[layout setSectionInset:UIEdgeInsetsMake(70.0/2208.0*HEIGHT, 70.0/1242.0*WIDTH, 100.0/2208.0*HEIGHT, 70.0/1242.0*WIDTH)];
//设置最小行间距
layout.minimumLineSpacing = 90.0/1242.0*WIDTH;
//最小列间距
layout.minimumInteritemSpacing = 60.0/2208.0*HEIGHT;
[self setCollectionView:[[UICollectionView alloc]initWithFrame: CGRectMake(0, 0, WIDTH, 640.0/2208.0*HEIGHT)collectionViewLayout:layout]];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
[_secondScrollView addSubview:_collectionView];
}
#pragma mark -UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//创建一个静态的字符串 当作单元格的标记
static NSString* cellId=@"cell";
//先表格视图的单元格池 中获取有该标记的单元格 获取到的单元格上暂时没有被使用的
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellId];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
return cell;
}
#pragma mark -UICollectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 20;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
[cell setBackgroundColor:[UIColor colorWithRed:arc4random () % 255 /255.0 green:arc4random () % 255 /255.0 blue:arc4random () % 255 /255.0 alpha:1]];
return cell;
}
@end
collectionView 和 tableView的嵌套使用的更多相关文章
- 关于collectionView和tableView的两种cell的出列方法的区别
相信好多人一定会对collectionView和tableView的两种cell出列方法有所疑问,下面以UICollection为例子进行举例说明 假设我们已经创建了一个collectionView, ...
- OC CollectionView和TableView自身高度的隐式递归计算,改变父试图布局
CollectionView和TableView自身高度的隐式递归计算 1.前沿:我们一般会碰到这样的需求,一个tableview或者一个colletionview放在一个scrollview上边,而 ...
- tableView的嵌套
1,subTableView需要开启多手势识别,多层tableView都会响应滚动事件(如果底层是scroll 依然会响应,这样滚动tableview时,scroll也会滚动,导致滚动过于灵活)2,通 ...
- tableView嵌套collectionView
首先是自定义collectionView填充的tableViewCell import UIKit // 定义一个collectionView,重写初始化大小和布局方法 class TrendsDet ...
- ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用
做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...
- ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动
两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 ...
- ios开发之-- tableview/collectionview获取当前点击的cell
方法如下: 一般collectionView 或者 tableview都有自带的点击函数,如下: , collectionView -(void)collectionView:(UICollectio ...
- iOS tableView嵌套部分WebView效果实现
对于一些资讯类的app,比如网易新闻,今日头条这样的,他们的文章详情页大部分基本都是tableView中嵌套webView来实现的效果,其中顶部标题,关注按钮等这些可能是原生的,内容部分是webVie ...
- iOS开发 爱特开发技术bug总结
#pragma mark 每天总结学习两小时 效率 和 每天学习 研究底层 多进去看看 // .................................................... ...
随机推荐
- for循环 重点题
1.冒泡排序 (特别重要): <script type="text/javascript"> var attr=Array(); for(var i=0; i< ...
- Ubuntu下用cue文件对ape和wav文件自动分轨
很多音乐CD的镜像文件都带cue格式的分轨文件,在Ubuntu Linux下可以通过一些工具来实现自动分轨. 一.Ubuntu下需要安装的工具有flac,shntool,libav-tools $ s ...
- JAVA下JSON的类型输出及使用
JSON类型的输出: import java.util.ArrayList; import java.util.HashMap; import net.sf.json.JSONArray; impor ...
- Ubuntu14.04配置3389远程桌面连接
1.安装xrdp sudo apt-get install xrdp -y 2.安装vnc4server sudo apt-get install vnc4server -y 3.安装xfce4 su ...
- Owl Carousel幻灯片插件的使用
Owl Carousel 是一个强大.实用但小巧的 jQuery 幻灯片插件,它具有一下特点: 兼容所有浏览器 支持响应式 支持 CSS3 过度 支持触摸事件 支持 JSON 及自定义 JSON 格式 ...
- h5、jq 移动端评论点攒功能
h5.jq 移动端评论点攒功能 平时做的项目中大部分都会涉及到评论的功能,之前用angular写的项目,功能写起来很方便,但是对于一个单页来说,angular有点大材小用了,所有今天分享一个关于jq制 ...
- (1)认识javascript
认识javascript 在本篇学习资料中,讲解javascript的基本概念.编写工具.在html中的使用: JavaScript 是脚本语言,这门语言可用于 HTML 和 web,更可广泛用于服务 ...
- oracle数据库的编码
--oracle数据库的编码select * from nls_database_parameters where parameter ='NLS_CHARACTERSET';--oracle客户端编 ...
- textarea禁止拖拽
<textarea style="resize:none;" ></textarea>'
- jquery判断对象是否显示或隐藏
if($('a.specail2').is(":visible")){ /**jquery判断对象是否显示或隐藏**/ $('a.one').hide(); $('a.specai ...