首先是自定义collectionView填充的tableViewCell

import UIKit

// 定义一个collectionView,重写初始化大小和布局方法
class TrendsDetailZanCollectionView: UICollectionView { var indexPath: NSIndexPath! override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
} required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
} } // collectionView的cellIdentity
let collectionViewCellIdentifier = "zanCollectionCell" class TrendsDetailZanCVTableViewCell: UITableViewCell { // tableViewCell中添加collectionView属性
var collectionView: TrendsDetailZanCollectionView! // 重写初始化方法,将collectionView加入tableViewCell中
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// 设置布局
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsMake(, , , ) //四周间距
layout.minimumLineSpacing =
layout.itemSize = CGSizeMake(, ) //每一个部分的size
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
self.collectionView = TrendsDetailZanCollectionView(frame: CGRectZero, collectionViewLayout: layout) // 初始化collectionView
self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewCellIdentifier)
self.collectionView.showsVerticalScrollIndicator = false
self.collectionView.backgroundColor = UIColor.whiteColor()
self.contentView.addSubview(self.collectionView) // 将collectionView加入tableView中
} required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
} // 注意⚠️重写子视图布局方法,将collectionView的大小设置为和tableViewCell大小相同
override func layoutSubviews() {
super.layoutSubviews()
let frame = self.contentView.bounds
self.collectionView.frame = CGRectMake(, , frame.size.width, frame.size.height)
} // 设置代理刷新数据, 记录下当前collectionView在tableView中的row或者indexPath
func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, index: NSInteger) {
self.collectionView.dataSource = delegate
self.collectionView.delegate = delegate
self.collectionView.tag = index
self.collectionView.reloadData()
} func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, indexPath: NSIndexPath) {
self.collectionView.dataSource = delegate
self.collectionView.delegate = delegate
self.collectionView.indexPath = indexPath
self.collectionView.tag = indexPath.section
self.collectionView.reloadData()
}
}

使用:

// tableView的cell,这里的identity是tableView的cellIdentity
let zanCellIdentifier = "zanTableViewCell"
tableView.registerClass(TrendsDetailZanCVTableViewCell.self, forCellReuseIdentifier: zanCellIdentifier)
let zanCell: TrendsDetailZanCVTableViewCell? = tableView.dequeueReusableCellWithIdentifier(zanCellIdentifier, forIndexPath: indexPath) as? TrendsDetailZanCVTableViewCell
tableView.separatorStyle = .None
return zanCell!
// 针对当前tableViewCell设置collectionView
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if !currentPinglunView {
if indexPath.row != {
if let collectionCell: TrendsDetailZanCVTableViewCell = cell as? TrendsDetailZanCVTableViewCell {
collectionCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, index: indexPath.row)
// 设置collectionView的内容偏移量
let index: NSInteger = collectionCell.collectionView.tag
// contentOffsetDictionary内容偏移量,在scrollView中有针对collectionView进行设置[因为垂直滑动,key为collectionView,value是x的偏移量]
let value: AnyObject? = self.contentOffsetDictionary.valueForKey(index.description)
let horizontalOffset: CGFloat = CGFloat(value != nil ? value!.floatValue : )
collectionCell.collectionView.setContentOffset(CGPointMake(horizontalOffset, ), animated: false)
}
}
}
}

写collectionView的代理方法

    // 首先有collectionView的代理方法
// MARK: - UICollectionView Data source and Delegate
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return zanUserIcons.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// 注意⚠️这里的cellIdentity和自定义的collectionViewCell的Identity一样!
let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("zanCollectionCell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.whiteColor()
let zanUserIconImageView = UIImageView.init(frame: CGRectMake(, , , ))
zanUserIconImageView.image = zanUserIcons[indexPath.item]
let zanuserNameLabel = UILabel.init(frame: CGRectMake(, , , ))
log.debug(cell.subviews.description)
// cell的重用,防重复添加子视图
if cell.subviews.count <= {
zanuserNameLabel.font = UIFont.systemFontOfSize()
zanuserNameLabel.text = zanuserNames[indexPath.item]
zanuserNameLabel.textColor = UIColor(hexString: "a8a8a8")
zanuserNameLabel.textAlignment = .Center
zanuserNameLabel.font = UIFont.systemFontOfSize()
cell.addSubview(zanUserIconImageView)
cell.addSubview(zanuserNameLabel)
}
// FIXME: - collectionCell高度计算
let indexP = NSIndexPath.init(forRow: , inSection: )
let maY = cell.frame.maxY
let maX = cell.frame.maxX
let x = SCREEN_WIDTH - maX
// 当前子视图在屏幕的最右时增加高度
if maY >= currentCollectionHeight || x < {
currentCollectionHeight = currentCollectionHeight + maY
if let cell = detailTableView.cellForRowAtIndexPath(indexP) {
cell.bounds.size.height = currentCollectionHeight
detailTableView.reloadRowsAtIndexPaths([indexP], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
log.debug("第\(collectionView.tag)行,第\(indexPath.item)个元素")
}
// tableView和collectionView代理都继承scrollView代理,滑动触发
func scrollViewDidScroll(scrollView: UIScrollView) {
if !scrollView.isKindOfClass(UICollectionView) {
return
}
let horizontalOffset: CGFloat = scrollView.contentOffset.x
let collectionView: UICollectionView = scrollView as! UICollectionView
self.contentOffsetDictionary.setValue(horizontalOffset, forKey: collectionView.tag.description)
}

tableView嵌套collectionView的更多相关文章

  1. ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动

    两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 ...

  2. tableViewCell嵌套collectionView,动态高度

    方法有很多,有通过内容高度,经过代理回调,刷新的,甚至还有计算cell个数,然后根据cell大小计算的,这里推荐iOS 8新特性,通过AutoLayout,利用内容将cell撑起来; 关键代码: vi ...

  3. iOS tableView嵌套部分WebView效果实现

    对于一些资讯类的app,比如网易新闻,今日头条这样的,他们的文章详情页大部分基本都是tableView中嵌套webView来实现的效果,其中顶部标题,关注按钮等这些可能是原生的,内容部分是webVie ...

  4. iOS 高效的分页加载(TableView、CollectionView)

    一.tableview的分页加载的代码对比 没有优化之前的代码如下 [strongSelf.tableView.mj_footer endRefreshing]: [strongSelf.articl ...

  5. 如何给TableView、CollectionView添加动效

    // // ViewController.m // tableViewAnimation // // Created by 冯敏 on 2018/3/13. // Copyright © 2018年 ...

  6. iOS 音视频播放

    播放控制切换为: ijkplayer wiki: https://github.com/changsanjiang/SJVideoPlayer/wiki/Use-ijkplayer 播放控制切换为: ...

  7. ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用

    做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...

  8. OC CollectionView和TableView自身高度的隐式递归计算,改变父试图布局

    CollectionView和TableView自身高度的隐式递归计算 1.前沿:我们一般会碰到这样的需求,一个tableview或者一个colletionview放在一个scrollview上边,而 ...

  9. IOS-当遇到tableView整体上移时的解决方案

    方案一在使用了navigationController后,当界面进行跳转往返后,时而会出现tableView或collectionView上移的情况,通常会自动上移64个像素,那么这种情况,我们可以关 ...

随机推荐

  1. 5.JSON

    AJAX传递复杂数据如果自己进行格式定义的话会经历组装.解析的过程,因此AJAX中有一个事实上的数据传输标准JSON. JSON将复杂对象序列化为一个字符串,在浏览器端再将字符串反序列化为JavaSc ...

  2. IEEE 802

    IEEE 802又称为LMSC(LAN /MAN Standards Committee, 局域网/城域网标准委员会), 致力于研究局域网和城域网的物理层和MAC层中定义的服务和协议, 对应OSI网络 ...

  3. activity跳转关闭软件盘

    之前试过 if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPE ...

  4. 博弈论(男人八题):POJ 1740 A New Stone Game

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5694   Accepted: 3119 ...

  5. 有关Spring Batch

    在使用Spring Batch时,在无法实现StepListener的情况下,如何使用ExecutionContext呢. 解决办法,使用宣言@BeforeStep或@AfterStep.

  6. Unity3d fur真实毛发渲染

    放出效果图 使用的核心技术为曲面细分和置换贴图,Unity支持GPU的曲面细分,置换贴图为噪波,沿着法线拉伸即成为毛发.再随机减少最高点的高度产生毛刺的感觉 曲面细分之前有篇文章详细讲过 弄了前后ri ...

  7. 关于python写GUI桌面应用的一些研究结果

    研究了一下python开发GUI桌面应用的解决方案,研究结果记录如下: EasyGui:控件极为简单,连个基本的grid.list组件都没有,不适合商用,甚至是普通的应用都不行,放弃! Tkinter ...

  8. 20169210《Linux内核原理与分析》第二周作业

    <Linux内核原理与分析>第二周作业 本周作业分为两部分:第一部分为观看学习视频并完成实验楼实验一:第二部分为看<Linux内核设计与实现>1.2.18章并安装配置内核. 第 ...

  9. (使用步骤)ThinkPHP3.1.2中如何配置Ckeditor_4.1.1和Ckfindtor(转)

    ThinkPHP3.1.2中如何配置Ckeditor_4.1.1和Ckfindtor  一.下载Ckeditor和Ckfinder Ckeditor官网 http://ckeditor.com/dow ...

  10. 编译LOADCEPC.EXE程序

    1.安装编译工具 安装MSVC152路径C:/MSVC; 安装MASM611可以自己指定E:/MASM611; 命令行编译 相关文件配置 修改setupen2.bat 如下: :PATH_DONE s ...