tableView镶嵌加入CollectionView实现方法
创建一个继承UICollectionView的类QHCollectionView
在QHCollectionView.h中添加接口方法
@interface QHCollectionView : UICollectionView
/**
* @frame: 外界传来的frame 即collectionView的大小
*
* @itemSize: 即collectionViewCell上的Item大小
*
* @imagerArr: 外界存放UIImage的数组
*/
- (instancetype)initWithFrame:(CGRect)frame collectionViewItemSize:(CGSize)itemSize withImageArr:(NSArray *)imagerArr;
@end
#import "QHCollectionView.h" #define cellID @"cellID"
@interface QHCollectionView ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) CGSize ItemSize;
@property (nonatomic, strong) NSArray *ImageArray;
@end
@implementation QHCollectionView
- (UICollectionViewFlowLayout *)layout {
if (!_layout) {
_layout = [[UICollectionViewFlowLayout alloc] init];
_layout.itemSize = self.ItemSize;
_layout.minimumLineSpacing = 10.0;
_layout.minimumInteritemSpacing = 0.0;
_layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_layout.sectionInset = UIEdgeInsetsMake(, , , );
}
return _layout;
}
- (instancetype)initWithFrame:(CGRect)frame collectionViewItemSize:(CGSize)itemSize withImageArr:(NSArray *)imagerArr {
_ItemSize = itemSize;
if (self = [super initWithFrame:frame collectionViewLayout:self.layout]) {
// [self setLayout:self.layout];
_ImageArray = imagerArr;
self.bounces = NO;
self.pagingEnabled = NO;
self.showsHorizontalScrollIndicator = NO;
self.delegate = self;
self.dataSource = self;
[self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
}
return self;
} #pragma mark - UICollectionViewDelegate --- UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.ImageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; // UIImageView *imageV = [[UIImageView alloc] initWithImage:_ImageArray[indexPath.row]];
CGRect imageFrame = imageV.frame;
imageFrame.size = _ItemSize;
imageV.frame = imageFrame;
[cell.contentView addSubview:imageV];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"第%ld分区--第%ld个Item", indexPath.section, indexPath.row);
} @end
最后在ViewController.m中导入头文件 以及调用
#import "ViewController.h"
#import "QHCollectionView.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end @implementation ViewController
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height) style:(UITableViewStylePlain)];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
return _tableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
UIImage *image1 = [UIImage imageNamed:@"1.jpg"];
UIImage *image2 = [UIImage imageNamed:@"2.jpg"];
UIImage *image3 = [UIImage imageNamed:@"3.jpg"];
UIImage *image4 = [UIImage imageNamed:@"4.jpg"];
NSArray *array = @[image1, image2, image3, image4, image1, image2, image3, image4];//可以自己导入喜欢的图片 QHCollectionView *CollectionView = [[QHCollectionView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, ) collectionViewItemSize:CGSizeMake(, ) withImageArr:array]; [cell.contentView addSubview:CollectionView];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
可以优化,创建一个类继承tableViewcell,重写,这里就不在上传代码了。
tableView镶嵌加入CollectionView实现方法的更多相关文章
- ios 关于collectionView.performBatchUpdates()方法 --时时更新
今天想实现一个简单的collectionView动画效果,查阅相关资料发现,实现 collectionView.performBatchUpdates()方法即可,于是掉坑里了. 文档: public ...
- collectionView代理方法快速设置cell大小上下左右间隔
#define JianGe 25 #define GeShu 4 #define ScreenWidth ([UIScreen mainScreen].bounds.size.width) #def ...
- ios7 tableview scrollsToTop 不执行处理方法
ios7中调用[self.tableview scrollsToTop] 没有效果(ios8中也没有效果) stackflow 处理方法: [self.tableviewscrollRectToVis ...
- 在UITableViewStylePlain情况下sectionHeader可以与tableview一起滑动的解决方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = ; ) { scrollVi ...
- ios中要在tableview中添加事件的方法
//触摸tableview背景响应事件. UIControl *bgcontrol = [[UIControl alloc]initWithFrame:self.tableView_typeList. ...
- 在ios程序中自己主动滚动TableView到某行的方法
比方tableview窗体能够显示 30 行, 我想在填充tableview 100 条数据后 选择第 50行, 能把这一行显示到窗体内, 就像手动拖滚动栏到 第 50行一样,要怎样实现呢? ] an ...
- 两个TableView产生联动的一中方法
如何使用两个TableView产生联动:将两个tableView的滚动事件禁止掉,最外层scrollView滚动时将两个TableView跟着滚动,并且更改contentOffset,这样产生效果滚动 ...
- ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用
做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...
- collectionView,tableView的细节处理
1.设置collectionView的高度 1.1为什么要设置高度? collectionView是在tableView的footView里面 , tableView能滚动,collectionVie ...
随机推荐
- el-upload控件一次接口请求上传多个文件
el-upload组件默认情况下上传多少个文件就会请求多少次上传接口,如何一次上传多个文件而不必多次请求上传接口呢?直接看代码 html <el-upload :action="act ...
- 搭建mock服务器(微信小程序)
搭建mock服务器(微信小程序) 如何在微信小程序使用mock.js实在是个问题,为了完全模拟访问路由和数据,选择在搭建本地mock服务器是一个不错的选择. 以下示例了一个mock服务器的搭建过程以及 ...
- C/C++程序基础 (二)常用知识点
使用宏实现max 注意括号在宏内的使用 #define MAX(x, y) ( ( (x) > (y) ) ? (x) : (y) ) 宏参数连接 a##e##b 转化为字符串 #a const ...
- 麦子学院python开发全套完整无加密课程
点击了解更多Python课程>>> 麦子学院python开发全套完整无加密课程 第一阶段:Python基础准备 1.Web前端开发之HTML+CSS基础入门 2.Javascript ...
- php同一个用户同时只能登陆一个, 后登陆者踢掉前登陆者(排他登陆)
通常用户登陆,如果没有特别的限定, 同一个用户可以同时登陆, 今天搞了一个东西限定一个用户不能同时登陆到一个系统上, 后登陆者会把前面登陆的踢出来.(有点像QQ,同个帐号不能在多个地方同时在线, 后面 ...
- python中函数返回多个值
用ipython运行情况如下: # 新建一个函数 In [1]: def nums(): ...: a = 11 ...: b = 22 ...: c = 33 ...: return [a,b,c] ...
- Python中的正则
regex_lst = [ ('字符组',), ('非打印字符',), ('特殊字符',), ('定位符',), ('限定符',), ('re模块',), ('分组命名',), ('或匹配',), ( ...
- PTA 数据结构——是否完全二叉搜索树
7-2 是否完全二叉搜索树 (30 分) 将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. 输入格 ...
- uncompyle2反编译python的.py文件
前几天学用github,一不小心把a.py文件给删除了,由于1天没有提交,也无法找回.突然发现同a.py文件生成的编译文件a.pyc还在,逐去搜索一番反编译的方法. 查询得知python比较好的工具u ...
- 微信小程序-----校园头条整体概括
1.项目需求 为了让在校师生可以更加方便的了解学校信息,从而合理的安排自己的时间,避免发生冲突和错过事件,通过小程序的便利性,可以达到随手一查的功能. 2.项目布局 3.效果展示 3.1登录 3.2首 ...