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 每天总结学习两小时 效率 和 每天学习 研究底层 多进去看看 // .................................................... ...
随机推荐
- 学习maven的各种问题
1. The container 'Maven Dependencies' references non existing library 解决方法,将eclipse中maven插件中“resolve ...
- 短信发送接口被恶意访问的网络攻击事件(三)定位恶意IP的日志分析脚本
前言 承接前文<短信发送接口被恶意访问的网络攻击事件(二)肉搏战-阻止恶意请求>,文中有讲到一个定位非法IP的shell脚本,现在就来公布一下吧,并没有什么技术难度,只是当时花了些时间去写 ...
- R语言重要数据集分析研究——搞清数据的由来
搞清数据的由来 作者:李雪丽 资料来源:百度百科
- oracle备份、还原
----第一步:设置空表导出 ----由于Oracle 11G在用EXPORT导出时,空表不能导出.解决方法如下:(11G中有个新特性,当表无数据时,不分配segment,以节省空间) select ...
- 宠物收养场 Treap
宠物收养场 时间限制: 1 Sec 内存限制: 128 MB 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠 ...
- SSH连接不上CentOS 主机配置文件导致的原因的解决方法
一.CentOS之SSH的安装与配置 SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定SSH 为建立在应用层和传输层基础上 ...
- Linux: 安装和启用firefox浏览器的java
之前在linux服务器上浏览一个网页时必须启用java才可以,但是自己只是一个服务器的普通用户,并没有root权限,所以只能把java装在自己的目录下面,因此不能用rpm包,而必须从Oracal官网下 ...
- Jenkins构建本地项目到服务器上自动部署的方法
博主原创,转载请注明. 最近在用Jenkins做项目的自动部署,由于项目需求,现在要在本地构建后再放到Tomcat里.以下是本地构建步骤: 名称填写好,下面的选项是可选的. 源码管理这里选择none. ...
- 【LeetCode】49. Group Anagrams
题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...
- 【LeetCode】119. Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...