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 ...
随机推荐
- ContentProvider 增删改查通讯录
一.通讯录应用介绍 通讯录应用是Android自带的应用程序,我们看到此应用的时候,可能只认为这是一个应用,用数据库存储数据,但是实际上不是这样的. 通讯录是ContentProvider的应用,通讯 ...
- 如何在asp.net中使用多线程及队列,异步处理一个耗时的任务(原创)
最近想在使用.net 的队列处理一些耗时的工作.经过考虑,需要先设计一个类,类中包含一个静态的队列.主要是写队列和读取队列. public class PaperCalculator { // 用于存 ...
- mysql的一个特殊问题 you can't specify target table 'cpn_regist' for update in FROM clause
今天在操作数据库的时候遇到了一个问题,sql语句如下: UPDATE cpn_yogurt_registration SET dep1Name = '1' WHERE `key` in (SELEC ...
- ubuntu14.04 的网络配置
为eth0 配置网络 vi /etc/network/intefaces 添加以下内容 auto eth0 iface eth0 inet static address 192.168.0.10 ne ...
- C# 从excel里面复制的1万6千多条记录粘贴到FCKeditor里面,点保存的时候,保存不了,页面没有反应
客户那边添加公告,是直接从excel里面复制的,有1万6千多条记录,excel文件有6M多. 编辑器用的FCKeditor,也能粘贴上,就是点保存的时候,执行了一段时间就没有反映了,保存不了. 想着可 ...
- 【笔记】CAP原理和BASE思想
摘自http://www.jdon.com/37625 分布式领域CAP理论,Consistency(一致性), 数据一致更新,所有数据变动都是同步的Availability(可用性), 好的响应性能 ...
- Swift +AFNetworking3.0 Get
let manager = AFHTTPSessionManager() let url = "http://v.juhe.cn/weather/index" let ," ...
- 根据显示的字符多少来做Label的自适应高度
根据显示的字符多少来做Label的自适应高度 UILabel *label = [[UILabel alloc]init]; NSString *string = @"其实,经年过往,每个人 ...
- C#保存Base64格式图片
.前端页面代码 /** * 通过图片本地路径获取图片真实大小,并进行压缩 */ function getLocalRealSize(path, callback) { var img = new Im ...
- Centos6.X下安装php nginx mysql 环境
---------------------------------------更换163软件源,此步可以省略,记得把repo文件里面的6.5改成当前版本号 yum makecache &&am ...