首先我们需要继承一下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的更多相关文章

  1. Swift UITableView嵌套UICollectionView点击事件冲突(点击事件穿透)

    不管是啥都响应tableviewcell class JYShopCertificationCell: UITableViewCell { override func hitTest(_ point: ...

  2. iOS使用UIScrollView实现左右滑动UITableView和UICollectionView

    在UIScrollView嵌套UITableView这篇文章是非常,但该项目的需求,需要嵌套UICollectionView,和UICollectionView和UITableView有非常多的不同, ...

  3. RumTime实践之--UITableView和UICollectionView缺省页的实现

    有关RunTime的知识点已经看过很久了,但是一直苦于在项目中没有好的机会进行实际运用,俗话说"光说不练假把式",正好最近在项目中碰到一个UITableView和UICollect ...

  4. UITableView和UICollectionView的方法学习一

    参考资料 UITableView UICollectionView UICollectionViewDataSource UICollectionViewDelegate UICollectionVi ...

  5. UITableView和UICollectionView的Cell高度的几种设置方式

    UITableViewCell 1.UITableView的Cell高度默认由rowHeight属性指定一个低优先级的隐式约束 2.XIB中可向UITableViewCell的contentView添 ...

  6. iOS 8自动调整UITableView和UICollectionView布局

    本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...

  7. iOS中UITableView和UICollectionView的默认空态页

    项目中想实现空态页风格统一控制的效果,就封装了一个默认空态页,使用的技术点有:1 方法替换 ,2 给分类(Category)添加属性. 我们知道,扩展(extension)可以给类添加私有变量和方法. ...

  8. UITableVIew与UICollectionView带动画删除cell时崩溃的处理

    UITableVIew与UICollectionView带动画删除cell时崩溃的处理 -会崩溃的原因是因为没有处理好数据源与cell之间的协调关系- 效果: tableView的源码: ModelC ...

  9. [转]iOS8 自动调整UITableView和UICollectionView布局

    转自:http://www.cocoachina.com/industry/20140825/9450.html (via:玉令天下的Blog)   本文讲述了UITableView.UICollec ...

随机推荐

  1. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  2. 从php脚本到浏览器,编码方式浅析

    今天简单看了一下php,服务器,浏览器如何设定编码 1.php 在php配置文件php.ini中,可以设置字符编码 ; PHP's default character set is set to em ...

  3. Android学习--摘录

    今天准备开始毕业设计--做一个android应用.无奈android基础木有一点,以前做过一个蓝牙应用,不过对android各方面的知识还是不懂啊,今天在android主页上看英文的android开发 ...

  4. iframe空文档中写入内容

    往一个空的iframe中写入内容,再其document ready之前有可能遇到拿回 的body指针为空,因此以下面的函数往其document中写入html HRESULT WriteToHtmlDo ...

  5. Excel VBA自动添加证书

    ---恢复内容开始--- 在说这个话题之前,我先解释一下为什么要加数字证书签名,它有什么作用,后面再解释如何添加.首先解释下证书添加的位置,如下图所示: 1.单击左上角的Office 按钮,选择右下角 ...

  6. 【07_226】Invert Binary Tree

    Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary ...

  7. lsof 简介

    lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传输控制协议 ...

  8. Hadoop-1.2.1 安装步骤小结(ubuntu)

    1.安装ubuntu系统 如果不使用云服务器,可以使用虚拟机WmWare安装,具体安装步骤这里就不讲了,ubuntu系统下载地址:http://www.ubuntu.com/download/desk ...

  9. HTTP协议状态码详解(HTTP Status Code)

    转自:http://www.cnblogs.com/shanyou/archive/2012/05/06/2486134.html 使用ASP.NET/PHP/JSP 或者javascript都会用到 ...

  10. c#中的static

    1.C# 不支持静态局部变量(在方法范围内声明的变量). 2.static类一般用于与状态无关的类.那么,什么是与状态无关的类?我的理解是当一个类中没有属性,只有方法的的时候,就可以认为这个类是与状态 ...