首先是自定义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. iis7 发布mvc3 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容及Login on failed for "IIS APPPOOL\ASP.NET v4.0"问题

    问题1: 发布mvc3报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 折腾了半天,提示里面的解决方法是: 如果不希望启用目录浏览,请确保配置了默认文档并且该文件存在. ...

  2. Selenium稳定性 Test

    [Test] public void DriverExtension_Wait() { var driver = new FirefoxDriver(); driver.Navigate().GoTo ...

  3. UVa 10917 A Walk Through the Forest

    A Walk Through the Forest Time Limit:1000MS  Memory Limit:65536K Total Submit:48 Accepted:15 Descrip ...

  4. An Easy Problem?! - POJ 2826(求面积)

    题目大意:有两块木板交叉起来接雨水,问最多能接多少.   分析:题目描述很简单,不过有些细节还是需要注意到,如下图几种情况:   #include<stdio.h> #include< ...

  5. 关于数据库一致改关闭下redo日志文件丢失的处理办法的总结

    数据库一致性关闭下redo日志文件丢失的处理办法(归档和非归档都行) 1. inactive log  在一致性关闭后删除重启时可以在mount下(不丢失数据) alter database clea ...

  6. 银联手机支付(.Net Csharp),3DES加密解密,RSA加密解密,RSA私钥加密公钥解密,.Net RSA 3DES C#

    前段时间做的银联支付,折腾了好久,拼凑的一些代码,有需要的朋友可以参考,本人.Net新手,不保证准确性! 这个银联手机支付没有SDK提供,技术支持也没有.Net的,真心不好搞! RSA加解密,这里有个 ...

  7. ios新特性

    @import  在xcode 5 下,为了更易于开发,增加了modules和 auto-linking 这两个新特性: 在以前,如果你要使用MapKit这个框架,你要这样做 1) 使用语句 #imp ...

  8. appium api

    AppiumDriver getAppStrings()      默认系统语言对应的Strings.xml文件内的数据.iOS driver.getAppStrings(Stringlanguage ...

  9. 关于封装unity3d的dll时候的进一步总结

    在进行新项目的开发或者接手.后期维护其他一些项目时,经常性的,我们会遇到以下情况: 1.使用新的插件替换已有的bug比较多或者不在维护的插件: 2.因策划需求或美术资源的变更,而不得不更换游戏中的游戏 ...

  10. Quartz.Net任务调度框架

    Quartz.Net是一个开源的任务调度框架,非常强大,能够通过简单的配置帮助我们定时具体的操作. 相对于我们用的线程里面while(true)然后sleep来执行某个操作,应该算的上是高端,大气,上 ...