uicollectionview用法的cell的间隔怎么不一致的

iOS开发: UICollectionView的使用(swift版,解决cell不显示) - 推酷
iOS开发: UICollectionView的使用(swift版,解决cell不显示)
UICollectionView类似于Android里的GridView,九宫格效果。使用起来也不算复杂,跟TableView差不多,之所以写篇博文,是因为地学习过程中遇到了一个大坑,希望对其他新人有所帮助。
环境:Xcode 6.1.1 Swift语言
新建SingleView Application,删除默认的ViewController类,清空Main.storyboard。往Main.storyboard拖进一个Collection View Controller,默认会有一个Collection View,往Collection View里拖进一个Collection View Cell,这个Cell就相当于是一个Item,接下来往Cell里随便拖个label,button之类的控件,用于测试。
选中cell,切换到Attributes inspector,将Identifier设为”cell”,无引号,字符串。这个id是用于复用item View的,跟Android的Adapter的复用机制一样。
选中Collection View Controller,切到Attributes inspector,将View Controller下的Is Initial View Controller勾选,这一步是声明这个View Controller是第一个ViewController,打开应用时展示这个。
新建Cocoa Touch Class,继承UICollectionViewController,名为CollectionViewController,打开会发现自动生成了一堆代码,很多已经注释掉,我们暂时先不管他们。注意几个方法:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -& Int {
//#warning Incomplete method implementation -- Return the number of sections
这个是Section数量,所谓Section就是分组。这里返回1,只有一组。
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -& Int {
//#warning Incomplete method implementation -- Return the number of items in the section
这个是对应分组的item数量,因为没有分组,不需要判断section,直接返回50
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -& UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
return cell
这个是最重要的方法,相当于Android的getView,返回一个item的View,reuseIdentifier就是我们之前在Main.storyboard给Cell定义的id,这里需要把reuseIdentifier常量改为”cell”,与前面定义相同。
运行… 发现问题了没?什么都没有显示…并没有展示我们之前往cell里拖的控件。坑就在这,根据Apple的文档,不能直接给UICollectionViewCell添加subView,而是要添加到UICollectionViewCell的contentView里,我们添加UICollectionViewCell时,并没有自动添加一个contentView,就相当于直接加到了UICollectionViewCell里。但是拖进UITableViewCell是有一个contentView的。这应该是xCode的一个bug吧,解决方法,只有用代码写cell的布局了.
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -& UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
//如果是复用的Cell,viewWithTag就不会返回nil
var label:UILabel? = cell.contentView.viewWithTag(TAG_CELL_LABEL) as? UILabel
if label == nil {
//非复用,创建并添加到contentView里
println(&label == nil\(indexPath.item)&)
label = UILabel(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
label!.tag = TAG_CELL_LABEL
cell.contentView.addSubview(label!)
label!.text = &\(indexPath.item)&
return cell
& 2014, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接!应用开发笔记
已发表评论数()
&&登&&&录&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见当前访客身份:游客 [
你才懒呢,你全家都懒
:很不错的文章
:引用来自“恶魔永生”的评论海哥霸气 哇咔咔 猜猜...
:海哥霸气 哇咔咔 猜猜我是谁。。。
:挺不错!多谢大牛
:居然没人回应 看上去不错的啊
今日访问:155
昨日访问:202
本周访问:549
本月访问:701
所有访问:21073
UICollectionView总结
发表于4个月前( 11:32)&&
阅读(275)&|&评论()
0人收藏此文章,
ios网格表单UICollectionView的使用方法
好久没写博客了,最近闲,多写点!
1.基本介绍
先介绍一下UICollectionView,大家应该都用过UITableView,不熟悉的可以看这里,UITableView中的表格只支持单排列表,没办法支持网格列表模式
当然也有很多大牛使用UITableView做出网格效果来了,实现的方式肯定都一样,就是将Cell分成几部分View,在赋值的时候一次性传两个或者多个data过去,通过delegate或者其他方式返回不同cell被点击的效果,要求具体Demo嘛,我找找看啊!
当然拉,上面的方法可行,但是麻烦,而且不好维护,在IOS6 SDK中就出了UICollectionView(只支持ios6以上系统),首先UICollectionView是基础UITableView的,所以UICollectionView的结构模式是和UITableView一模一样的。
2.使用方法
UICollectionView的使用方法和UITableView的基本相似,不同的是UICollectionViewFlowLayout和UICollectionViewCell
&&&&UICollectionViewFlowLayout&*flowLayout&=&[[UICollectionViewFlowLayout&alloc]&init];
&&&&[flowLayout&setItemSize:CGSizeMake(CAPTURE_SIZE/2,&CAPTURE_SIZE/2)];
&&&&[flowLayout&setScrollDirection:UICollectionViewScrollDirectionVertical];
&&&&flowLayout.sectionInset&=&UIEdgeInsetsMake(10,&10,&10,&10);
&&&&flowLayout.minimumLineSpacing&=&10;
&&&&flowLayout.minimumInteritemSpacing&=&0;
&&&&flowLayout.footerReferenceSize&=&CGSizeMake(300,&30);
&&&&_collectionView.delegate&=&
&&&&_collectionView.dataSource&=&
&&&&[_collectionView&setCollectionViewLayout:flowLayout];
&&&&[_collectionView&registerClass:[RDImgeCollectionCell&class]&forCellWithReuseIdentifier:@"RDImgeCollectionCell"];
&&&&[_collectionView&setBackgroundColor:[UIColor&whiteColor]];
UICollectionViewFlowLayout决定了UICollectionViewCell将要显示的大小,间隔,排列方式等
#pragma&-mark&CollectionView&DataSource
-&(NSInteger)collectionView:(UICollectionView&*)collectionView&numberOfItemsInSection:(NSInteger)section{
&&&&return&_myRandoImageArray.
-&(UICollectionViewCell&*)collectionView:(UICollectionView&*)collectionView&cellForItemAtIndexPath:(NSIndexPath&*)indexPath{
&&&&static&NSString&*identifier&=&@"RDImgeCollectionCell";
&&&&RDImgeCollectionCell&*cell&=&(RDImgeCollectionCell&*)[collectionView&dequeueReusableCellWithReuseIdentifier:identifier&forIndexPath:indexPath];
&&&&RDImageModel&*image&=&[_myRandoImageArray&objectAtIndex:indexPath.row];
&&&&[cell.imageView&setImageWithURL:[NSURL&URLWithString:image.imageUrl]&placeholderImage:[UIImage&imageNamed:@"defaultImage"]];
&&&&[cell&showLikeMsg:image.likeNumber];
&&&&return&
#pragma&-mark&CollectionView&UICollectionViewDelegate
-&(void)collectionView:(UICollectionView&*)collectionView&didSelectItemAtIndexPath:(NSIndexPath&*)indexPath
&&&&RDImageModel&*image&=&[_myRandoImageArray&objectAtIndex:indexPath.row];
&&&&RDDetailImageViewController&*detailImageViewController&=&[[RDDetailImageViewController&alloc]&init];
&&&&detailImageViewController.imageArray&=&_myRandoImageA
&&&&detailImageViewController.thisImage&=&
&&&&detailImageViewController.isMyImage&=&YES;
&&&&[_navController&pushViewController:detailImageViewController&animated:YES];
两个delegate和tableView的类型,返回datasouce和处理响应的事件!
用到UICollectionView的一个小项目Rando地址:
1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读今天看啥 热点:
ios开发——解决UICollectionView的cell间距与设置不符问题
在用UICollectionView展示数据时,有时我们希望将cell的间距调成一个我们想要的&#20540;,然后查API可以看到有这么一个属性:
- (CGFloat)minimumInteritemSpacing {
然而很多情况下我们会发现,这样写不能满足我们的要求,cell之间仍然有一个不知道怎么产生的间距。
我们知道cell的间距是由cell的大小itemSize和section的缩进sectionInset共同决定的,通过这两个数据,UICollectionView动态地将cell放在相应的位置,然而即使我们接着调用inset方法也没有用。
- (UIEdgeInsets)sectionInset
return UIEdgeInsetsMake(0, 0, 0, 0);
可以看到,继承UICollectionViewFlowLayout后在- layoutAttributesForElementsInRect:方法中打印一下这些cell的frame的结果
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
NSMutableArray* attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for (UICollectionViewLayoutAttributes *attr in attributes) {
NSLog(@"%@", NSStringFromCGRect([attr frame]));
从上面两行就可以看出来,我的高度是42.8,然而两个cell的x&#20540;间距却为46,也就是说有大约3px的间距。
其实,正如minimumInteritemSpacing的名字一样,这个属性设置的是间距的最小&#20540;,那么,我们实际需要的应该是一个"maximumInteritemSpacing",也就是最大间距。要解决这个问题,需要自己做一些计算。
依旧是继承UICollectionViewFlowLayout,然后在- layoutAttributesForElementsInRect:方法中添加如下代码:
//从第二个循环到最后一个
for(int i = 1; i < [attributes count]; ++i) {
//当前attributes
UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];
//上一个attributes
UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[i - 1];
//我们想设置的最大间距,可根据需要改
NSInteger maximumSpacing = 0;
//前一个cell的最右边
NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
//如果当前一个cell的最右边加上我们想要的间距加上当前cell的宽度依然在contentSize中,我们改变当前cell的原点位置
//不加这个判断的后果是,UICollectionView只显示一行,原因是下面所有cell的x值都被加到第一行最后一个元素的后面了
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.
frame.origin.x = origin + maximumS
currentLayoutAttributes.frame =
原因注释中已经解释得非常详细了。这样就可以解决cell的间距问题了。再次运行,效果非常好:
相关搜索:
相关阅读:
相关频道:
IOS教程最近更新08:07 提问
iOS: UICollectionViewcell里面的图片不能加载
跟实例基本差不多,但每次都显示image对象为nil,请问大神哪里有问题?
ViewController.m
PhotoViewer
Created by yonzhang on 13-10-13.
Copyright (c) 2013年 yonzhang. All rights reserved.
#import "ViewController.h"
#import "ImageCell.h"
@interface ViewController ()
@property NSMutableArray *photoA
@implementation ViewController
(void)viewDidLoad
[super viewDidLoad];
self.collectionView
self.collectionview.delegate =
self.collectionview.dataSource =
[self.collectionview registerClass: [ImageCell class]
forCellWithReuseIdentifier:@"Cell"];
self.collectionview.dataSource =
// Do any additional setup after loading the view, typically from a nib.
(void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
#pragma Collection View Data Souce
(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImage *testImage = [UIImage imageNamed:@"4.JPG"];
cell.image.image = [UIImage imageNamed:@"4.JPG"];
#pragma custommethod
按赞数排序
你的cell.image应该是UIImageView吧
如果是UIImageView的话,先把你的名称改了,image太特殊了,容易与系统命名重名,这句demo我感觉你应该用SDWebimage
cell.image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"4.JPG"]];
其他相似问题
相关参考资料}

我要回帖

更多关于 uicollectionview用法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信