ios中collectionview不走代理方法怎么回事

 我来答
yourscool又香
2016-07-13 · TA获得超过376个赞
知道小有建树答主
回答量:3367
采纳率:0%
帮助的人:559万
展开全部
layout的是自定义的一个layout
这是layout的。m
#import "HeadPageViewLayout.h"
CGFloat const colCount = 3;
@interface HeadPageViewLayout ()
@property (nonatomic, weak) id <WaterFLayoutDelegate> delegate;
@property (nonatomic, strong) NSMutableArray *columnHeights;
@property (nonatomic, strong) NSMutableArray *sectionItemAttributes;
@property (nonatomic, strong) NSMutableArray *allItemAttributes;
@property (nonatomic, strong) NSMutableArray *unionRects;
@property (nonatomic, assign) CGFloat itemWidth;

@end
@implementation HeadPageViewLayout
const NSInteger unionSize = 20;
//懒加载
- (void)setColumnCount:(NSInteger)columnCount
{
if (_columnCount != columnCount)
{

_columnCount = columnCount;
[self invalidateLayout];
}

}
- (void)setMinimumColumnSpacing:(CGFloat)minimumColumnSpacing
{
if (self.minimumColumnSpacing != minimumColumnSpacing) {
self.minimumColumnSpacing = minimumColumnSpacing;
[self invalidateLayout];
}
}
- (void)setMinimumInteritemSpacing:(CGFloat)minimumInteritemSpacing
{
if (_minimumInteritemSpacing != minimumInteritemSpacing) {
_minimumInteritemSpacing = minimumInteritemSpacing;
[self invalidateLayout];
}
}
- (void)setSectionInset:(UIEdgeInsets)sectionInset
{
if (!UIEdgeInsetsEqualToEdgeInsets(_sectionInset, sectionInset)) {
_sectionInset = sectionInset;
[self invalidateLayout];
}
}
#pragma mark - Private Accessors

- (NSMutableArray *)unionRects
{
if (!_unionRects) {
_unionRects = [NSMutableArray array];

}
return _unionRects;
}
- (NSMutableArray *)columnHeights
{
if (!_columnHeights) {
_columnHeights = [NSMutableArray array];
}
return _columnHeights;
}
- (NSMutableArray *)allItemAttributes
{
if (!_allItemAttributes) {
_allItemAttributes = [NSMutableArray array];

}
return _allItemAttributes;
}
- (NSMutableArray *)sectionItemAttributes
{
if (!_sectionItemAttributes) {
_sectionItemAttributes = [NSMutableArray array];
}
return _sectionItemAttributes;
}
//懒加载结束
#pragma mark - init
//初始化
- (void)commonInit
{
_columnCount = 2;
_minimumInteritemSpacing = 10;
_minimumColumnSpacing = 10;
_sectionInset = UIEdgeInsetsZero;
}
- (id)init
{
if (self = [super init]) {
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self commonInit];
}
return self;
}

- (void)prepareLayout//准备布局
{
[super prepareLayout];
// [self.collectionView numberOfItemsInSection:100];
NSInteger numberOfSections = [self.collectionView numberOfSections];
if (numberOfSections == 0)
{
return;
}
self.delegate = (id <WaterFLayoutDelegate> )self.collectionView.delegate;//集合视图协议
NSInteger idx = 0;
//.sectionInset属性 设定全局的区内边距
CGFloat width = self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right;

//
self.itemWidth = floorf((width - (self.columnCount - 1) * self.minimumColumnSpacing) / self.columnCount);
[self.unionRects removeAllObjects];
[self.columnHeights removeAllObjects];
[self.allItemAttributes removeAllObjects];
[self.sectionItemAttributes removeAllObjects];
for (idx = 0; idx < self.columnCount; idx++)
{
[self.columnHeights addObject:@(0)];
}
CGFloat top = 0;
UICollectionViewLayoutAttributes *attributes;//属性,特征

for (NSInteger section = 0; section < numberOfSections; ++section)
{
top += self.sectionInset.top;
for (idx = 0; idx < self.columnCount; idx++)
{
self.columnHeights[idx] = @(top);
}
/*
* 2. Section items
*/
NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
NSMutableArray *itemAttributes = [NSMutableArray arrayWithCapacity:itemCount];

// Item will be put into shortest column.
for (idx = 0; idx < itemCount; idx++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:section];
CGSize itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
CGFloat itemHeight = floorf(itemSize.height * self.itemWidth / itemSize.width);
NSUInteger columnIndex = [self shortestColumnIndex];
CGFloat xOffset = self.sectionInset.left + (self.itemWidth + self.minimumColumnSpacing) * columnIndex;
CGFloat yOffset = [self.columnHeights[columnIndex] floatValue];

attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = CGRectMake(xOffset, yOffset, self.itemWidth, itemHeight);
[itemAttributes addObject:attributes];
[self.allItemAttributes addObject:attributes];
self.columnHeights[columnIndex] = @(CGRectGetMaxY(attributes.frame) + self.minimumInteritemSpacing);
}
[self.sectionItemAttributes addObject:itemAttributes];
for (idx = 0; idx < self.columnCount; idx++)
{
self.columnHeights[idx] = @(top);
}
} // end of for (NSInteger section = 0; section < numberOfSections; ++section)
idx = 0;
NSInteger itemCounts = [self.allItemAttributes count];
while (idx < itemCounts) {
CGRect rect1 = ((UICollectionViewLayoutAttributes *)self.allItemAttributes[idx]).frame;
idx = MIN(idx + unionSize, itemCounts) - 1;
CGRect rect2 = ((UICollectionViewLayoutAttributes *)self.allItemAttributes[idx]).frame;
[self.unionRects addObject:[NSValue valueWithCGRect:CGRectUnion(rect1, rect2)]];
idx++;

}
}
- (CGSize)collectionViewContentSize
{
NSInteger numberOfSections = [self.collectionView numberOfSections];
if (numberOfSections == 0)
{
return CGSizeZero;
}

CGSize contentSize = self.collectionView.bounds.size;
contentSize.height = [self.columnHeights[0] floatValue];

return contentSize;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
{
if (path.section >= [self.sectionItemAttributes count])
{
return nil;
}
if (path.item >= [self.sectionItemAttributes[path.section] count])
{
return nil;
}
return (self.sectionItemAttributes[path.section])[path.item];
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSInteger i;
NSInteger begin = 0, end = self.unionRects.count;
NSMutableArray *attrs = [NSMutableArray array];

for (i = 0; i < self.unionRects.count; i++)
{
if (CGRectIntersectsRect(rect, [self.unionRects CGRectValue]))
{
begin = i * unionSize;
break;
}
}
for (i = self.unionRects.count - 1; i >= 0; i--)
{
if (CGRectIntersectsRect(rect, [self.unionRects CGRectValue]))
{
end = MIN((i + 1) * unionSize, self.allItemAttributes.count);
break;
}
}
for (i = begin; i < end; i++)
{
UICollectionViewLayoutAttributes *attr = self.allItemAttributes;
if (CGRectIntersectsRect(rect, attr.frame))
{
[attrs addObject:attr];
}
}

return [NSArray arrayWithArray:attrs];
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
CGRect oldBounds = self.collectionView.bounds;
if (CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds) ||
CGRectGetHeight(newBounds) != CGRectGetHeight(oldBounds))
{
return YES;
}
return NO;
}
#pragma mark - Private Methods

/**
* Find the shortest column.
*
* @return index for the shortest column
*/
- (NSUInteger)shortestColumnIndex
{
__block NSUInteger index = 0;
__block CGFloat shortestHeight = MAXFLOAT;

[self.columnHeights enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
CGFloat height = [obj floatValue];
if (height < shortestHeight)
{
shortestHeight = height;
index = idx;
}
}];

return index;
}

/**
* Find the longest column.
*
* @return index for the longest column
*/
- (NSUInteger)longestColumnIndex
{
__block NSUInteger index = 0;
__block CGFloat longestHeight = 0;

[self.columnHeights enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
CGFloat height = [obj floatValue];
if (height > longestHeight)
{
longestHeight = height;
index = idx;
}
}];

return index;
}

@end
这是。h
@protocol WaterFLayoutDelegate <UICollectionViewDelegate>
@required
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
@optional
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForHeaderInSection:(NSInteger)section;

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForFooterInSection:(NSInteger)section;

@end

@interface HeadPageViewLayout : UICollectionViewLayout

@property (nonatomic, assign) NSInteger columnCount;//这个布局有多少列,默认2列
@property (nonatomic, assign) CGFloat minimumColumnSpacing;//列最小间距
@property (nonatomic, assign) CGFloat minimumInteritemSpacing;//同一列中两个item的最小间距
@property (nonatomic, assign) UIEdgeInsets sectionInset;//
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式