首先是自定义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. WITH AS 优化逻辑读

    SQL> select * from fxqd_list_20131115_new where (acct_no, oper_no, seqno, trans_amt) not in (sele ...

  2. Android Fragment实现分屏

    在项目中碰到一个问题,新开发一个平板APP,项目要求是把原来的一个手机端APP放在项目左侧显示,右侧添加新加的功能. 首先想到了Fragment,以前做过Fragment的一些简单的Demo,但是都没 ...

  3. sql2005如何附加数据库

    1.首先把mdf(数据库主文件)和ldf(数据库日志文件)放到C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data 2.登陆sq ...

  4. html/php, 二个文本框求和,在第三个框中显示

    我想要实现的是第三个文本框本来输出的是默认值,按了提交按钮之后,显示了一个我通过php某个计算后想要输出的值,如何实现?就好比说:我输入两个数,我按了个提交按钮之后,那个第三个文本框本来输出是“输出框 ...

  5. [zz]android的logcat详细用法

    Android日志系统提供了记录和查看系统调试信息的功能.日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat 命令来查看和使用.      一.使用logcat命令的目的: ...

  6. Centos 下安装MongoDB

    Centos 下安装MongoDB 一.安装方法 方法(一) 1  配置包管理系统 创建/etc/yum.repos.d/mongodb.repo 文件,当然我们使用的是64位系统,32位的情况不再考 ...

  7. kafka consumer频繁reblance

    转载请注明地址http://www.cnblogs.com/dongxiao-yang/p/5417956.html 结论与下文相同,kafka不同topic的consumer如果用的groupid名 ...

  8. c#基础语言编程-编码

    字符编码是计算机技术的基础理论,其字符编码有ASCII码.UTF-8.还有就是GB2312,当然这是在中国常用的. 1.ASCII码 在计算机内部所有的信息都是以二进制字符进行存储.用每个二进制位中的 ...

  9. thymeleaf 和其它标签组合 获取数据

    thymeleaf 有很多的内置标签, 但是我们在开发中会引入其它很多标签, 这个时候, 后台数据过来了,前端 页面要怎么显示呢? 网上资料真的很少. 不过还是找到了答案:  th:attr 这个标签 ...

  10. 自己在安装centos 系统时, 是使用英文安装 成功,现在系统语言为英语,如何设置为中文?

    作为一个linux菜鸟,遇到的问题可谓真多,在虚拟机VMware上安装好centos系统后,心里甚喜,也连上网络了. 一.遇到的问题 but,火狐浏览器浏览网页出现乱码,也不知道怎么解决?所有的中文都 ...