首先是自定义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. elasticsearch spring 集成

    elasticsearch spring 集成 项目清单   elasticsearch服务下载包括其中插件和分词   http://download.csdn.net/detail/u0142011 ...

  2. BZOJ1135: [POI2009]Lyz

    1135: [POI2009]Lyz Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 264  Solved: 106[Submit][Status] ...

  3. 挖坑#3-----DP优化+CDQ分治+期望DP

    1492: [NOI2007]货币兑换Cash 1176: [Balkan2007]Mokia 1452: [JSOI2009]Count 1563: [NOI2009]诗人小G tyvj1309   ...

  4. HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  5. git_share

    linux 环境(192.168.8.58) 1. 生成rsa key $ ssh-keygen 如果你之前没有跑过这个文件, 接受默认选项即可. 这样你会在 ~/.ssh/下看到 id_rsa和id ...

  6. [svn] 数据库操作残留,无法进行操作的解决方法

    WINDOWS环境下的解决方法: 1: 下载sqlite3数据库工具,放置于SVN的同级目录 2: CMD路径转移到Sqlite3目录 3: 残留操作选择: sqlite3 .svn/wc.db &q ...

  7. nginx简单双机热备:backup参数的使用

    nginx简单双机热备:backup参数的使用 nginx简单双机热备:backup参数的使用

  8. scrapy使用代理

    import base64 # Start your middleware class class ProxyMiddleware(object): # overwrite process reque ...

  9. Struts2配置文件讲解

    解决在断网环境下,配置文件无提示的问题我们可以看到Struts.xml在断网的情况下,前面有一个叹号,这时,我们按alt+/ 没有提示,这是因为” http://struts.apache.org/d ...

  10. maven tomcat1.7环境下构建javaweb 项目

    tomcat用户权限设置 在tomcat安装路径\conf目录下tomcat-users.xml添加: <role rolename="admin-gui"/> < ...