UITableViewCell中嵌套UICollectionView
来源:互联网 发布:淘宝图片超链接 编辑:程序博客网 时间:2024/05/23 19:15
- 自定义UITableViewCell,用于添加UICollectionView
- 自定义UICollectionView,添加属性监听点击CollectionView内容时所在第行数
- 需将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
效果如下:
阅读全文
0 0
- UITableViewCell中嵌套UICollectionView
- UITableViewCell中嵌套UICollectionView
- UITableViewCell中嵌套UITableView,用UITextView加载HTML数据
- UITableViewCell嵌套UIWebView
- UITableViewCell嵌套UIWebView
- UITableViewCell嵌套UIWebView
- UITableViewCell嵌套UIWebView
- UITableViewCell嵌套UIWebView
- UITableViewCell和UICollectionView的几种动画
- UIScrollView嵌套UITableView和UICollectionView
- UITableViewCell 嵌套 UIWebView,cell自适应高度
- iOS学习笔记:UITableViewcell 嵌套 UIWebView
- UITableViewCell嵌套UITableView的正确姿势
- iOS UITableViewCell嵌套UIWebView 自适应高度
- iOS中 UICollectionView UI_19
- uitableviewcell内嵌入uicollectionview后uitableviewcell高度随 uicollection的contentsize的变化而变化
- UITableViewCell中单行选择
- UITableViewCell中添加控件
- 常用的Mysql数据库操作语句大全
- intellj 使用
- android 3.3 butterknife-7.0.1。jar(com.jakewharton:butterknife 7.0.1)
- web
- PAT——1061. 判断题(15)
- UITableViewCell中嵌套UICollectionView
- RAC环境下修改字符集
- c primer plus 练习一
- ffmpeg图片转视频设置-r丢帧问题
- 【Linux内核】timer子系统
- Java POI 导出EXCEL
- spring的事务有几种方式,谈谈spring事务的隔离级别和传播行为
- ③看视频NDK学习之C常用函数
- iOS 打电话弹出太慢