创建一个继承UICollectionView的类QHCollectionView
在QHCollectionView.h中添加接口方法
1 @interface QHCollectionView : UICollectionView 2 /** 3 * @frame: 外界传来的frame 即collectionView的大小 4 * 5 * @itemSize: 即collectionViewCell上的Item大小 6 * 7 * @imagerArr: 外界存放UIImage的数组 8 */ 9 - (instancetype)initWithFrame:(CGRect)frame collectionViewItemSize:(CGSize)itemSize withImageArr:(NSArray *)imagerArr; 10 @end
1 #import "QHCollectionView.h" 2 3 #define cellID @"cellID" 4 @interface QHCollectionView ()<UICollectionViewDelegate, UICollectionViewDataSource> 5 @property (nonatomic, strong) UICollectionViewFlowLayout *layout; 6 @property (nonatomic, assign) CGSize ItemSize; 7 @property (nonatomic, strong) NSArray *ImageArray; 8 @end 9 @implementation QHCollectionView 10 - (UICollectionViewFlowLayout *)layout { 11 if (!_layout) { 12 _layout = [[UICollectionViewFlowLayout alloc] init]; 13 _layout.itemSize = self.ItemSize; 14 _layout.minimumLineSpacing = 10.0; 15 _layout.minimumInteritemSpacing = 0.0; 16 _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; 17 _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); 18 } 19 return _layout; 20 } 21 - (instancetype)initWithFrame:(CGRect)frame collectionViewItemSize:(CGSize)itemSize withImageArr:(NSArray *)imagerArr { 22 _ItemSize = itemSize; 23 if (self = [super initWithFrame:frame collectionViewLayout:self.layout]) { 24 // [self setLayout:self.layout]; 25 _ImageArray = imagerArr; 26 self.bounces = NO; 27 self.pagingEnabled = NO; 28 self.showsHorizontalScrollIndicator = NO; 29 self.delegate = self; 30 self.dataSource = self; 31 [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID]; 32 } 33 return self; 34 } 35 36 #pragma mark - UICollectionViewDelegate --- UICollectionViewDataSource 37 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 38 return self.ImageArray.count; 39 } 40 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 41 UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 42 [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; // 43 44 UIImageView *imageV = [[UIImageView alloc] initWithImage:_ImageArray[indexPath.row]]; 45 CGRect imageFrame = imageV.frame; 46 imageFrame.size = _ItemSize; 47 imageV.frame = imageFrame; 48 [cell.contentView addSubview:imageV]; 49 return cell; 50 } 51 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 52 NSLog(@"第%ld分区--第%ld个Item", indexPath.section, indexPath.row); 53 } 54 55 @end
最后在ViewController.m中导入头文件 以及调用
1 #import "ViewController.h" 2 #import "QHCollectionView.h" 3 @interface ViewController ()<UITableViewDelegate, UITableViewDataSource> 4 @property (nonatomic, strong) UITableView *tableView; 5 @end 6 7 @implementation ViewController 8 - (UITableView *)tableView { 9 if (!_tableView) { 10 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:(UITableViewStylePlain)]; 11 _tableView.delegate = self; 12 _tableView.dataSource = self; 13 [self.view addSubview:_tableView]; 14 } 15 return _tableView; 16 } 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])]; 20 } 21 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 22 return 200; 23 } 24 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 25 return 2; 26 } 27 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 28 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath]; 29 UIImage *image1 = [UIImage imageNamed:@"1.jpg"]; 30 UIImage *image2 = [UIImage imageNamed:@"2.jpg"]; 31 UIImage *image3 = [UIImage imageNamed:@"3.jpg"]; 32 UIImage *image4 = [UIImage imageNamed:@"4.jpg"]; 33 NSArray *array = @[image1, image2, image3, image4, image1, image2, image3, image4];//可以自己导入喜欢的图片 34 35 QHCollectionView *CollectionView = [[QHCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) collectionViewItemSize:CGSizeMake(100, 180) withImageArr:array]; 36 37 [cell.contentView addSubview:CollectionView]; 38 return cell; 39 } 40 - (void)didReceiveMemoryWarning { 41 [super didReceiveMemoryWarning]; 42 // Dispose of any resources that can be recreated. 43 } 44 45 @end
可以优化,创建一个类继承tableViewcell,重写,这里就不在上传代码了。