tableView镶嵌加入CollectionView实现方法

时间:2024-01-02 22:40:20

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