UITableViewCell中嵌套UICollectionView

来源:互联网 发布:淘宝图片超链接 编辑:程序博客网 时间:2024/05/23 19:15
  1. 自定义UITableViewCell,用于添加UICollectionView
  2. 自定义UICollectionView,添加属性监听点击CollectionView内容时所在第行数
  3. 需将UITableView的代理和UICollectionView的代理放置在同一个Controller里,以便后续操作

自定义的UICollectionView如下:

@interface CustCollectionView : UICollectionView@property (nonatomic, strong) NSIndexPath *rowIndex;@end

自定义的UITableViewCell如下:

// CustTableCell.h 代码@interface CustTableCell : UITableViewCell@property (nonatomic, strong) CustCollectionView *collection;@end// CustTableCell.m 代码@implementation CustTableCell- (UICollectionView *)collection{    if (!_collection) {        // 初始化UICollectionView的布局对象        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];        // 指定内容编辑距离        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);        // 指定UICollectionViewCell的大小(宽高皆是80)        layout.itemSize = CGSizeMake(80, 80);        // 指定滚动方向(水平)        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;        // 指定CollectionView的高度和宽度,以及在UITableViewCell中的位置        _collection = [[CustCollectionView alloc] initWithFrame:CGRectMake(0, 45, [UIScreen mainScreen].bounds.size.width, 100) collectionViewLayout:layout];        _collection.backgroundColor = [UIColor lightGrayColor];    }    return _collection;}-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        [self.contentView addSubview:self.collection];    }    return self;}@end

控制器代码如下:

@interface WDYListCollectController () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>@property (nonatomic, strong) UITableView *tableView;@end@implementation WDYListCollectController- (void)viewDidLoad {    [super viewDidLoad];    [self initUI];}- (void)initUI{    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];    self.tableView.delegate = self;    self.tableView.dataSource = self;    // 注册cell    [self.tableView registerClass:[CustTableCell class] forCellReuseIdentifier:@"tableIdentifier"];    [self.view addSubview:self.tableView];}#pragma mark - UITableView的代理方法-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 5;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    CustTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableIdentifier"];    return cell;}// 将UITableViewCell显示之前,给对应的collection设置响应的属性- (void)tableView:(UITableView *)tableView willDisplayCell:(CustTableCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    cell.collection.delegate = self;    cell.collection.dataSource = self;    [cell.collection registerClass:[WDYCollectionViewCell class] forCellWithReuseIdentifier:@"collectionIdentifier"];    // 这里还需要给cell.collection设置是属于哪一行的rowIndex    cell.collection.rowIndex = indexPath;}// 指定UITableViewCell的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 145.0;}#pragma mark - UICollectionView的代理方法-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 5;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    WDYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionIdentifier" forIndexPath:indexPath];    return cell;}- (void)collectionView:(CustCollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"行数:%d,collectio位置:%d ", (int)collectionView.rowIndex.row, (int)indexPath.row);}@end

效果如下:

这里写图片描述

原创粉丝点击