uicollectionview详解Cell能设置对齐方式吗

IOS UItableview UIcollectionview cell高度自适应
时间: 20:57:46
&&&& 阅读:267
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&1.tableviewcell 高度自适应
& &主要方法:
  计算cell高度[cell.contentview&systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
& & & //&NSLayoutConstraint 使用条件 添加相对约束
& ios 7.0 cell 高度计算修改:
&  - (CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath。 设置cell 高度估值
&  - (CGFloat)tableView:(UITableView )tableView HeightForRowAtIndexPath:(NSIndexPath )indexPath& 计算cell 显示高度
& & & 在ios7之后 官方修改防止加载大量数据时计算所有cell的高度后才加载数据。
  计算高度需要返回值需要+1.
2.uicollectioncell 高度自适应&
&  主要方法:
  计算cell高度[cell&systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
& & & //&NSLayoutConstraint 使用条件 添加相对约束
&标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&& &&&&&&
&& && && &&&&&&
版权所有 鲁ICP备号-4
打开技术之扣,分享程序人生!iOS8自定义Collection View Cell - Swift教程 - swift迷
Collection View提供了一个灵活方式展示集合视图,用法有点类似Table View类.使用Collection view可以实现网格或者实现任何你能想象到的布局。在这篇教程中将实现自定义collection view cell,教程在iOS8&Xcode6下编译通过。
打开Xcode,新建项目选择Single View Application,Product Name填写IOS8SwiftCustomCollectionViewCellsTutorial,Organization Name和Organization Identifier自行填写,选择与iPhone设备。
我们需要在自定义的Collection View Cell中展示一张图,下载图片并添加至工程
打开Storyboard,移除ViewController并拖拽 collection view
Controller至界面.由于我们移除了初始的ViewController,因此默认启动的界面没了,选中Table View Controller然后到Attribute Inspector控制面板勾选“Is Initial View Controller”选项,如下图:
选中Collection View同时打开Size Inspector面板,将Cell的大小设置为50x50
拖拽Image View至Collection View Cell中并确保高宽都为50,选中Image View并到Attribute Inspector选择Mode为“Aspect Fit”
现在Storyboard大致如下:
这时ViewController.swift已经不需要了我们将它删除掉。接下来,在工程中新增一个文件,选择 iOS-&Source-&Cocoa Touch Class,新建一个继承UICollectionViewController名为CollectionViewController类:
现在我们将新建的类与Storyboard的Collection View Controller 进行关联,打开Storyboard且选中Collection View Controller,然后到Identity Inspector控制面板改变Custrom Class为新建的类,如下图:
打开CollectionViewController.swift文件,在viewDidLoad方法中删除如下行
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
打开Storyboard选中Collection View Cell设置Identifier为"Cell"
接下来创建Collection View Cell自定义class,在工程中新增一个文件,选择 iOS-&Source-&Cocoa Touch Class,新建一个继承UICollectionViewCell名为CollectionViewCell类
打开Storyboard选中CollectionViewCell设置自定的Class为CollectionViewCell
打开Assistant Editor并确保CollectionViewCell.swift 可见,Ctrl+Drag方式给ImageView创建如下Outlet
打开 CollectionViewController.swift 文件增加如下属性
var myImage = UIImage(named: "Apple_Swift_Logo")
改变如下代码:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -& Int {
// Return the number of sections
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -& Int {
// Return the number of items in the section
return 100
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -& UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell
// Configure the cell
cell.imageView.image = myImage
return cell
1.设置CollectionView的section数量为1
2.Collection View的cells数量为100
3.设置CollectionViewCelll的imageView为myimage
编译并运行项目,效果如下:
feiin 于 6个月前
Swift开发交流QQ群:主题 : UIcollectionView cell高度不同,宽度相同,怎么设置同列间距保持一致?
级别: 新手上路
可可豆: 0 CB
威望: 1 点
在线时间: 1(时)
发自: Web Page
UIcollectionView cell高度不同,宽度相同,怎么设置同列间距保持一致?&&&
cell的宽度相同,高度不同 &我需要同列的cell 间距保持一致 。而同行的cell无需对齐flowLayout.minimumInteritemSpacing&=&0; 设置为0也没用,似乎同一行的cell会保持中心点一致。弄了很久也不会,真心求大神帮忙
下面的图就是目前的情况,而我想做到同一列的间距一致
级别: 新手上路
UID: 302097
可可豆: 342 CB
威望: 252 点
在线时间: 85(时)
发自: Web Page
我觉得你可以考虑,根据图片的尺寸大小动态调整cell上面各个subview的位置。试试.
级别: 新手上路
可可豆: 0 CB
威望: 1 点
在线时间: 1(时)
发自: Web Page
cell上的subview位置应该没问题吧,现在是cell与cell之间的列距无法调整 同一行的cell貌似一直都是处于同一个中心点
cell的高度是根据图片大小动态返回的
级别: 新手上路
UID: 302097
可可豆: 342 CB
威望: 252 点
在线时间: 85(时)
发自: Web Page
我觉得你可以考虑,根据图片的尺寸大小动态调整cell上面各个subview的位置。试试.
级别: 新手上路
UID: 302097
可可豆: 342 CB
威望: 252 点
在线时间: 85(时)
发自: Web Page
所以:我的建议是cell的高度不用根据图片大小调整(所有cell均用同一个高度),而是cell里面的subview根据图片的尺寸大小动态调整;以达到对齐的效果.
级别: 新手上路
UID: 302097
可可豆: 342 CB
威望: 252 点
在线时间: 85(时)
发自: Web Page
我觉得你可以考虑,根据图片的尺寸大小动态调整cell上面各个subview的位置。试试.
级别: 新手上路
可可豆: 78 CB
威望: 88 点
在线时间: 324(时)
发自: Web Page
楼主解决了吗,我也遇到这种了,求指教
关注本帖(如果有新回复会站内信通知您)
论坛技术问题应该发布到? 正确答案:CocoaChina问答
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版UICollectionView的使用方法及demo - 推酷
UICollectionView的使用方法及demo
直接上代码,说明请看注释吧
ViewController.h
#import &UIKit/UIKit.h&
@interface ViewController : UIViewController&UICollectionViewDataSource,UICollectionViewDelegateFlowLayout&{
@property (strong, nonatomic)UICollectionView *collectionV
ViewController.m
#import &ViewController.h&
@interface ViewController ()
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
//确定是水平滚动,还是垂直滚动
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, 320, 200) collectionViewLayout:flowLayout];
self.collectionView.dataSource=
self.collectionView.delegate=
[self.collectionView setBackgroundColor:[UIColor clearColor]];
//注册Cell,必须要有
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@&UICollectionViewCell&];
[self.view addSubview:self.collectionView];
#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return 10;
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
static NSString * CellIdentifier = @&UICollectionViewCell&;
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
label.textColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@&%d&,indexPath.row];
for (id subView in cell.contentView.subviews) {
[subView removeFromSuperview];
[cell.contentView addSubview:label];
#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
return CGSizeMake(60, 60);
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
return UIEdgeInsetsMake(5, 5, 5, 5);
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//临时改变个颜色,看好,只是临时改变的。如果要永久改变,可以先改数据源,然后在cellForItemAtIndexPath中控制。(和UITableView差不多吧!O(∩_∩)O~)
cell.backgroundColor = [UIColor greenColor];
NSLog(@&item======%d&,indexPath.item);
NSLog(@&row=======%d&,indexPath.row);
NSLog(@&section===%d&,indexPath.section);
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
return YES;
已发表评论数()
&&登&&&录&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见

我要回帖

更多关于 uicollectioncell 的文章

 

随机推荐