创建一个继承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,重写,这里就不在上传代码了。

最新文章

  1. BPM SharePoint解决方案分享
  2. javadoc相关问题
  3. [lucene系列笔记1]lucene6的安装与配置(Windows系统)
  4. Spring MVC 学习总结(七)——FreeMarker模板引擎与动态页面静态化
  5. MAC下利用Github 、hexo、 多说、百度统计 建立个人博客指南
  6. DOM--5 动态修改样式和层叠样式表
  7. td内元素居顶,td元素不随高度的撑开而变位置
  8. 使用Unity开发Android的几种调试方法
  9. cocoapods安装失败
  10. Altera quartus II遇到的问题
  11. Android编程心得-Service数据绑定初步
  12. SpringMVC通过实体类返回json格式的字符串,并在前端显示
  13. 从Google Play下载应用并不安全,上千款监视软件伪装其中
  14. 【Alpha阶段】第六次scrum meeting
  15. canvas实现倒计时效果示例(vue组件内编写)
  16. Xcode8出现的一些常见问题
  17. js-图片轮播(极简)
  18. 微信JSAPI分享朋友圈调试经验:invalid signature签名错误排查
  19. SpringMvc中获取Request
  20. 蓝牙学习笔记三(Android Debug)

热门文章

  1. 初尝微信小程序2-Swiper组件、导航栏标题配置
  2. C++ 学习笔记 (八)重载 重写 重定义以及名字覆盖
  3. 抽屉head部分,hover应用,鼠标放上变色
  4. 项目十八-Hadoop+Hbase分布式集群架构“完全篇”
  5. 利用PHPExcel 实现excel数据的导入导出(源码实现)
  6. php五种常见的设计模式
  7. 关于web.xml配置中的&lt;url-pattern&gt;
  8. Paul Zindel【保罗&#183;金代尔】
  9. Jack Straws POJ - 1127 (简单几何计算 + 并查集)
  10. nova boot添加volume_type参数支持