UItableView嵌套UICollectionView
首先我们需要继承一下UITableView并且遵守<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>的代理实现响应的代理方法,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{}
-(MedCView *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}
^ /*
| 下方这个代理方法,在一般开发中并不常见,但是这里不可获取
| */
| -(void)tableView:(UITableView *)tableView willDisplayCell:(MedCView *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{}
|
| <- 箭头指向的自定义tableView相信你也一定注意到了,这里想要实现嵌套就需要自定义,自定义的tableview需要实现同时实现继承UItableview和UICollectionview
这就需要实现两个接口
//方便复制:.h的定义接口
#import <UIKit/UIKit.h>
@interface MedColView : UICollectionView
@property (nonatomic, strong) NSIndexPath *indexPath;
@end
static NSString *MedCViewCellIdentifier = @"MedCViewCellIdentifier";
@interface MedCView : UITableViewCell
@property (nonatomic, strong) MedColView *collectionView;
-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath ;
@end
//.m方法的实现
#import "MedCView.h"
#import "MedCollView.h"
#import "define.h"
@implementation MedColView
@end
@interface MedCView()
@property (nonatomic,strong)UICollectionViewFlowLayout *layout;
@end
static NSString *CollectionViewCell = @"CollectionViewCell";
float H;
@implementation MedCView
-(UICollectionViewFlowLayout *)layout{
if (_layout == nil) {
_layout = [[UICollectionViewFlowLayout alloc] init];
_layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
_layout.itemSize = CGSizeMake(ZCScreenWidth /4,95/H);
_layout.minimumLineSpacing = 0.1;
_layout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return _layout;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
H =[UIView hightsize];
self.collectionView = [[MedColView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:self.layout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCell];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.showsVerticalScrollIndicator = NO;
[self addSubview:self.collectionView];
self.collectionView.scrollEnabled = YES;
self.collectionView.scrollsToTop = YES;
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.collectionView.frame = CGRectMake(0, self.contentView.bounds.origin.y, self.contentView.bounds.size.width , self.contentView.bounds.size.height);
}
-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
{
self.collectionView.dataSource = dataSourceDelegate;
self.collectionView.delegate = dataSourceDelegate;
self.collectionView.indexPath = indexPath;
[self.collectionView setContentOffset:self.collectionView.contentOffset animated:NO];
[self.collectionView reloadData];
[[NSNotificationCenter defaultCenter]postNotificationName:@"sethight" object:nil];
}
@end
//setCollectionViewDataSourceDelegate:方法是为了实现代理方法的传递与前方的-(void)tableView:(UITableView *)tableView willDisplayCell:(MedCView *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{}方法呼应
//由于这只是我的一个Controller里的一个模块,所以发了一个通知去更新UI,这里的难度就是同时继承两个控件实现各自的代理方法以及各类之间的关联度也很高
UItableView嵌套UICollectionView的更多相关文章
- Swift UITableView嵌套UICollectionView点击事件冲突(点击事件穿透)
不管是啥都响应tableviewcell class JYShopCertificationCell: UITableViewCell { override func hitTest(_ point: ...
- iOS使用UIScrollView实现左右滑动UITableView和UICollectionView
在UIScrollView嵌套UITableView这篇文章是非常,但该项目的需求,需要嵌套UICollectionView,和UICollectionView和UITableView有非常多的不同, ...
- RumTime实践之--UITableView和UICollectionView缺省页的实现
有关RunTime的知识点已经看过很久了,但是一直苦于在项目中没有好的机会进行实际运用,俗话说"光说不练假把式",正好最近在项目中碰到一个UITableView和UICollect ...
- UITableView和UICollectionView的方法学习一
参考资料 UITableView UICollectionView UICollectionViewDataSource UICollectionViewDelegate UICollectionVi ...
- UITableView和UICollectionView的Cell高度的几种设置方式
UITableViewCell 1.UITableView的Cell高度默认由rowHeight属性指定一个低优先级的隐式约束 2.XIB中可向UITableViewCell的contentView添 ...
- iOS 8自动调整UITableView和UICollectionView布局
本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...
- iOS中UITableView和UICollectionView的默认空态页
项目中想实现空态页风格统一控制的效果,就封装了一个默认空态页,使用的技术点有:1 方法替换 ,2 给分类(Category)添加属性. 我们知道,扩展(extension)可以给类添加私有变量和方法. ...
- UITableVIew与UICollectionView带动画删除cell时崩溃的处理
UITableVIew与UICollectionView带动画删除cell时崩溃的处理 -会崩溃的原因是因为没有处理好数据源与cell之间的协调关系- 效果: tableView的源码: ModelC ...
- [转]iOS8 自动调整UITableView和UICollectionView布局
转自:http://www.cocoachina.com/industry/20140825/9450.html (via:玉令天下的Blog) 本文讲述了UITableView.UICollec ...
随机推荐
- 图解 & 深入浅出 JavaWeb:Servlet必会必知
从[JavaEE 要懂的小事] Http相关,一直想写点Web开发相关的.最近项目接口开发紧,还有准备新的九月份战斗.JDK IO源码就隔一段落,温故知新看看Servlet & JSP 相关. ...
- SQL中的循环、for循环、游标
我们使用SQL语句处理数据时,可能会碰到一些需要循环遍历某个表并对其进行相应的操作(添加.修改.删除),这时我们就需要用到咱们在编程中常常用的for或foreach,但是在SQL中写循环往往显得那么吃 ...
- URLEncoder编码
客户端在进行网页请求的时候,网址中可能会包含非ASCII码形式的内容,比如中文. 而直接把中文放到网址中请求是不允许的,所以需要用URLEncoder编码地址, 将网址中的非ASCII码内容转换成可以 ...
- PLSQL数据导入导出问题解决(空表、大字段表、表空间错误等)
PLSQL使用方法简单,平常使用较多,但在平常使用过程中,遇到一些问题,下面简单罗列并进行解决.这些解决方法大多通过网络查找获得,这里只是进行简单整理. 使用的数据库版本为:Oracle11g. 通用 ...
- 使用Dapper时,如何将MySqlParameters[] 变成Dapper.DynamicParameters动态对象
在开发过程中,如何替原生的参数化MySqlParameters[]换成Dpapper的参数化DynamicParameters对象正确的做法:var args = new DynamicParamet ...
- instr函数
在Oracle/PLSQL中,instr函数返回要截取的字符串在源字符串中的位置. 语法如下:instr( string1, string2 [, start_position [, nth_appe ...
- WPF中设置快捷键
方式1: 窗体中加入资源 <UserControl.Resources> <RoutedUICommand x:Key="Cut" Text=&qu ...
- Wpf 中使用gif格式的动态图
第一种方法:使用winform插件 <WindowsFormsHost xmlns:wf="clr-namespace:System.Windows.Forms;assembly=S ...
- 权限<九>
介绍 角色就是相关权限的命令集合,使用角色的主要目的就是为了简化权限的管理,假定有用户 a,b,c 为了让他们都拥有权限 1. 连接数据库 2. 在 scott.emp 表上 select,ins ...
- Ubuntu 14.04中gedit打开文件出现中文乱码问题
http://blog.csdn.net/cywosp/article/details/32325449/ 在中文支持配置还不完整的Ubuntu 14.04中,使用gedit打开带有中文字符的文件有时 ...