collectionview可以添加静态cell吗 像tableviewcell一样

方法有很多,有通过内容高度,经过代理回调,刷新的,甚至还有计算cell个数,然后根据cell大小计算的,这里推荐iOS 8新特性,通过AutoLayout,利用内容将cell撑起来;
viewcontroller中:
self.tableview.estimatedRowHeight = 40;
self.tableview.rowHeight = UITableViewAutomaticD
cell中:这里要说明,给collectionview上下左右添加约束,再加上高度约束,就是通过更新collectionview的高度约束,将cell撑起来的
& & cell根据数据源赋值方法中:
& & self.frame = [UIScreen mainScreen].
& & [self setNeedsLayout];
& & [self layoutIfNeeded];
& & [self.collectionView reloadData];
& & [self updateTheCollectionview:self.collectionView.collectionViewLayout.collectionViewContentSize.height];
阅读(...) 评论()collectionview可以添加静态cell吗 像tableview一样? ?
如果不能 怎么能实现一些想要在collectionview中设置静态cell的需求 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 网友回复:
collectionview可以添加静态cell吗 像tableview一样? ?
【collectionview可以添加静态cell吗 像tableview一样? ?】
请将本文分享给你的朋友:
collectionview可以添加静态cell吗 像tableview一样? ? 的相关文章
------分隔线----------------------------
北京联盟郑重声明:本文仅代表作者个人观点,与北京联盟无关。其原创性及文中陈述内容未经本站证实,北京联盟对本文及其中全部或者部分内容的真实性、完整性、及时性不作任何保证和承诺,请网友自行核实相关内容。TableViewCell内嵌collectionView
先上效果图这篇内容和上篇/p/b5346ebb8b28TableViewCell内嵌ScrollView内容用的方法相似,因为UICollectionView继承UIScrollView,所以用法都差不多下面上代码:创建一个继承UICollectionView的类QHCollectionView在QHCollectionView.h中添加接口方法
@interface QHCollectionView : UICollectionView
@frame: 外界传来的frame 即collectionView的大小
@itemSize: 即collectionViewCell上的Item大小
@imagerArr: 外界存放UIImage的数组
- (instancetype)initWithFrame:(CGRect)frame collectionViewItemSize:(CGSize)itemSize withImageArr:(NSArray *)imagerA
在QHCollectionView.m中
#import "QHCollectionView.h"
#define cellID @"cellID"
@interface QHCollectionView ()&UICollectionViewDelegate, UICollectionViewDataSource&
@property (nonatomic, strong) UICollectionViewFlowLayout *
@property (nonatomic, assign) CGSize ItemS
@property (nonatomic, strong) NSArray *ImageA
@implementation QHCollectionView
- (UICollectionViewFlowLayout *)layout {
if (!_layout) {
_layout = [[UICollectionViewFlowLayout alloc] init];
_layout.itemSize = self.ItemS
_layout.minimumLineSpacing = 10.0;
_layout.minimumInteritemSpacing = 0.0;
_layout.scrollDirection = UICollectionViewScrollDirectionH
_layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
- (instancetype)initWithFrame:(CGRect)frame collectionViewItemSize:(CGSize)itemSize withImageArr:(NSArray *)imagerArr {
_ItemSize = itemS
if (self = [super initWithFrame:frame collectionViewLayout:self.layout]) {
[self setLayout:self.layout];
_ImageArray = imagerA
self.bounces = NO;
self.pagingEnabled = NO;
self.showsHorizontalScrollIndicator = NO;
self.delegate =
self.dataSource =
[self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
#pragma mark - UICollectionViewDelegate --- UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.ImageArray.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; //
UIImageView *imageV = [[UIImageView alloc] initWithImage:_ImageArray[indexPath.row]];
CGRect imageFrame = imageV.
imageFrame.size = _ItemS
imageV.frame = imageF
[cell.contentView addSubview:imageV];
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"第%ld分区--第%ld个Item", indexPath.section, indexPath.row);
最后在ViewController.m中导入头文件 以及调用
#import "ViewController.h"
#import "QHCollectionView.h"
@interface ViewController ()&UITableViewDelegate, UITableViewDataSource&
@property (nonatomic, strong) UITableView *tableV
@implementation ViewController
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:(UITableViewStylePlain)];
_tableView.delegate =
_tableView.dataSource =
[self.view addSubview:_tableView];
return _tableV
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
UIImage *image1 = [UIImage imageNamed:@"12.jpg"];
UIImage *image2 = [UIImage imageNamed:@"19.jpg"];
UIImage *image3 = [UIImage imageNamed:@"25.jpg"];
UIImage *image4 = [UIImage imageNamed:@"29.jpg"];
NSArray *array = @[image1, image2, image3, image4, image1, image2, image3, image4];
QHCollectionView *CollectionView = [[QHCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) collectionViewItemSize:CGSizeMake(100, 180) withImageArr:array];
[cell.contentView addSubview:CollectionView];
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
有什么不懂得可以提问最后附上Demo:/catcups/TableViewAddCollectionView
qh_1预览.png首先是自定义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(15, 20, 0, 20) //四周间距
layout.minimumLineSpacing = 27
layout.itemSize = CGSizeMake(62, 62) //每一个部分的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(0, 0, 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 != 0 {
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 : 0)
collectionCell.collectionView.setContentOffset(CGPointMake(horizontalOffset, 0), 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(10, 0, 44, 44))
zanUserIconImageView.image = zanUserIcons[indexPath.item]
let zanuserNameLabel = UILabel.init(frame: CGRectMake(0, 52, 62, 10))
log.debug(cell.subviews.description)
// cell的重用,防重复添加子视图
if cell.subviews.count &= 1 {
zanuserNameLabel.font = UIFont.systemFontOfSize(10)
zanuserNameLabel.text = zanuserNames[indexPath.item]
zanuserNameLabel.textColor = UIColor(hexString: "a8a8a8")
zanuserNameLabel.textAlignment = .Center
zanuserNameLabel.font = UIFont.systemFontOfSize(10)
cell.addSubview(zanUserIconImageView)
cell.addSubview(zanuserNameLabel)
// FIXME: - collectionCell高度计算
let indexP = NSIndexPath.init(forRow: 1, inSection: 0)
let maY = cell.frame.maxY
let maX = cell.frame.maxX
let x = SCREEN_WIDTH - maX
// 当前子视图在屏幕的最右时增加高度
if maY &= currentCollectionHeight ||
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) {
let horizontalOffset: CGFloat = scrollView.contentOffset.x
let collectionView: UICollectionView = scrollView as! UICollectionView
self.contentOffsetDictionary.setValue(horizontalOffset, forKey: collectionView.tag.description)
阅读(...) 评论()tableViewCell、collectionViewCell、组头组尾等总结_IOS开发-织梦者
当前位置:&>&&>& > tableViewCell、collectionViewCell、组头组尾等总结
tableViewCell、collectionViewCell、组头组尾等总结
使用xib方式自定义cell
tableView使用代码方式自定义cell
collectionView使用代码方式自定义cell
使用xib方式自定义cell:
1.创建一个自定义的cell类,并继承UITableViewCell
2.设置xib文件里面的cell需要和自定义的cell相关联(在类型控制器中更改class)
3.设置xib文件中属性控制器下的可重用ID,要和自定义cell中的相同
4.拖线生成属性
5.给cell定义一个模型属性,并重写该模型属性的set方法
<img alt="tableViewCell、collectionViewCell、组头组尾等总结" src="http://blog.csdn.net/my_Wade/article/details/cid:0C628F26-71A1-014D-9A0A-27F23ACC16CA.png" width="669" height="107" width="541" height="218" width="779" height="162" width="576" height="299" width="480" height="194" width="651" height="186" width="641" height="311" width="590" height="262" width="626" height="112" width="714" height="342" width="669" height="212" width="737" height="370" width="650" height="199" width="680" height="310" width="677" height="421" width="684" height="349" width="660" height="334" width="724" height="397" width="480" height="154" width="724" height="386" width="673" height="488" width="738" height="383" width="480" height="135" width="667" height="484" width="735" height="392" width="480" height="123" width="700" height="376" width="480" height="151" width="648" height="312" width="834" height="379" width="873" height="404"
以上就是tableViewCell、collectionViewCell、组头组尾等总结的全文介绍,希望对您学习和使用ios应用开发有所帮助.
这些内容可能对你也有帮助
更多可查看IOS开发列表页。
猜您也会喜欢这些文章

我要回帖

更多关于 静态tableviewcell 的文章

 

随机推荐