集合试图(UICollectionview)的灵活使用

2017-12-12  本文已影响0人  a457e74fe3a4

1.相信大家都会很熟练使用UITableView了,但是今天给大家 介绍一个iOS6就已经引入的UICollectionView ,功能更加强大,更加灵活,学会使用它,会 帮你解决很多的问题的!它跟UITableView很相似,所以 熟练使用UICollectionView很简单!

2 。先列举一个简单的例子


效果图1.png

我们可以看到这个流式布局,我们可以自定义 布局,继承与UICollectionViewFlowLayout

我现在将 主要代码 贴出来 ,希望对正在使用UICollectionView的同学有所帮助

自定义 布局,继承与 UICollectionViewFlowLayout
CoustomFlowLayOut.m

/**

/**

/**
UICollectionViewLayoutAttributes attrs;
1.一个cell对应一个UICollectionViewLayoutAttributes对象
2.UICollectionViewLayoutAttributes对象决定了cell的frame
/
/

/**

*/

自定义cell的话,就是只有一张图片,这里不再赘述,控制器里
mainViewController.m

pragma mark - <UICollectionViewDataSource>

pragma mark - <UICollectionViewDelegate>

上边就是 主要的代码,就可以很简单的实现 上边图片的功能

大家还可能 见过商城中的 商品中的布局 ,类似是这样的,


效果图.png

其实上边的这种效果也很容易实现,只要自己 自定义布局就行了,只是这个布局比 上边那种布局更复杂一点,但是整体的思路还是相同的,就是 自定义 布局,自定义cell,然后控制器 来利用使用它们,在这里 我将主要的代码贴出来,让大家参考,希望对大家 深入了解认识UICollectionView有好的帮助
同样的我先贴出 自定义layOut的 代码
.h中

import <UIKit/UIKit.h>

@class MKMasonryViewLayout;

@protocol MKMasonryViewLayoutDelegate <NSObject>
@required

@interface MKMasonryViewLayout : UICollectionViewLayout
@property (nonatomic, assign) NSUInteger numberOfColumns;
@property (nonatomic, assign) CGFloat interItemSpacing;
@property (weak, nonatomic) IBOutlet id<MKMasonryViewLayoutDelegate> delegate;
@end

.m中

import "MKMasonryViewLayout.h"

@interface MKMasonryViewLayout (/Private Methods/)
@property (nonatomic, strong) NSMutableDictionary *lastYValueForColumn;
@property (strong, nonatomic) NSMutableDictionary *layoutInfo;
@end

@implementation MKMasonryViewLayout

-(void) prepareLayout {

//集合试图 布局 前 会调用这个方法,在这里将 布局物件的边框都计算好 缓存到某个地方

self.numberOfColumns = 3;
self.interItemSpacing = 12.5;

CGFloat currentColumn = 0;
CGFloat fullWidth = self.collectionView.frame.size.width;
CGFloat availableSpaceExcludingPadding = fullWidth - (self.interItemSpacing * (self.numberOfColumns + 1));
CGFloat itemWidth = availableSpaceExcludingPadding / self.numberOfColumns;//每个物件的宽度

self.lastYValueForColumn = [NSMutableDictionary dictionary];

self.layoutInfo = [NSMutableDictionary dictionary];
NSIndexPath *indexPath;
NSInteger numSections = [self.collectionView numberOfSections];

for(NSInteger section = 0; section < numSections; section++) {
//collectionView 是 横着 排列的,而不是 竖着排列的
NSInteger numItems = [self.collectionView numberOfItemsInSection:section];
//每个 section 上含有几个 nuMItems
for(NSInteger item = 0; item < numItems; item++){
indexPath = [NSIndexPath indexPathForItem:item inSection:section];

  UICollectionViewLayoutAttributes *itemAttributes =
  [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  
  CGFloat x = self.interItemSpacing + (self.interItemSpacing + itemWidth) * currentColumn;
  CGFloat y = [self.lastYValueForColumn[@(currentColumn)] doubleValue];
  
  CGFloat height = [((id<MKMasonryViewLayoutDelegate>)self.collectionView.delegate)
                    collectionView:self.collectionView
                    layout:self
                    heightForItemAtIndexPath:indexPath];
  
  itemAttributes.frame = CGRectMake(x, y, itemWidth, height);
  y+= height;
  y += self.interItemSpacing;
  
  self.lastYValueForColumn[@(currentColumn)] = @(y);
  
  currentColumn ++;
  if(currentColumn == self.numberOfColumns) currentColumn = 0;
  self.layoutInfo[indexPath] = itemAttributes;
}

}
}

-(CGSize) collectionViewContentSize {

NSUInteger currentColumn = 0;
CGFloat maxHeight = 0;
do {
CGFloat height = [self.lastYValueForColumn[@(currentColumn)] doubleValue];
if(height > maxHeight)
maxHeight = height;
currentColumn ++;
} while (currentColumn < self.numberOfColumns);

return CGSizeMake(self.collectionView.frame.size.width, maxHeight);
}

@end

上边就是 完整的把想要的布局 给 布置好了,同样自定义cell这里也不 多谢,现在把 控制器的代码贴出来!

import "SCTViewController.h"

@interface SCTViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, MKMasonryViewLayoutDelegate, UICollectionViewDelegateFlowLayout>

@end

@implementation SCTViewController

// this will be called if our layout is UICollectionViewFlowLayout

// this will be called if our layout is MKMasonryViewLayout

@end

上边的代码就可以实现上图中所要实现的功能,希望可以帮到个位!希望个位提出宝贵意见!

上一篇 下一篇

猜你喜欢

热点阅读