1.出現(xiàn)問題:
項(xiàng)目中UICollectionViewCell的寬度為 屏幕寬度 / 7懈玻,且最小間距已設(shè)置為0 , 但是展示的cell間總有莫名其妙的分割線 , 如下圖:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
CGFloat itemWidth = self.frame.size.width / 7;
layout.itemSize = CGSizeMake(itemWidth, itemWidth);
layout.minimumInteritemSpacing = 0;
2.測(cè)試原因:
創(chuàng)建新類 FYCalendarCellFlowLayout 繼承 UICollectionViewFlowLayout,重寫下面方法古话,打印每個(gè)cell的frame迅箩,結(jié)果如下:
該方法官方解釋:返回所有布局屬性
return an array layout attributes instances for all the views in the given rect
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray* attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for (UICollectionViewLayoutAttributes *attr in attributes) {
NSLog(@"%@", NSStringFromCGRect([attr frame]));
}
return attributes;
}
此時(shí)我用的iPhone5s的模擬器缠沈,寬320维蒙,每個(gè)cell的寬度約等于45.7(并未除盡)儿倒,而2個(gè)cell的x軸的間距卻不完全等于這個(gè)數(shù)版保,也就是會(huì)有間距。
{{0, 0}, {45.714285714285715, 45.714285714285715}}
{{45.5, 0}, {45.714285714285715, 45.714285714285715}}
{{91.5, 0}, {45.714285714285715, 45.714285714285715}}
{{137, 0}, {45.714285714285715, 45.714285714285715}}
{{183, 0}, {45.714285714285715, 45.714285714285715}}
{{228.5, 0}, {45.714285714285715, 45.714285714285715}}
{{274.5, 0}, {45.714285714285715, 45.714285714285715}}
{{0, 45.5}, {45.714285714285715, 45.714285714285715}}
{{45.5, 45.5}, {45.714285714285715, 45.714285714285715}}
{{91.5, 45.5}, {45.714285714285715, 45.714285714285715}}
......
3.解決辦法:
依舊是重寫上述方法夫否,添加如下代碼, 最后使用FYCalendarCellFlowLayout創(chuàng)建UICollectionView彻犁,再次運(yùn)行,解決問題凰慈。
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray* attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
//從第2個(gè)循環(huán)到最后一個(gè)
for(int i = 1; i < [attributes count]; ++i) {
//當(dāng)前attributes
UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];
//上一個(gè)attributes
UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[i - 1];
//設(shè)置最大間距汞幢,可根據(jù)需要改
NSInteger maximumSpacing = 0;
//前一個(gè)cell的最右邊
NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
//如果當(dāng)前一個(gè)cell的最右邊加上我們想要的間距加上當(dāng)前cell的寬度依然在contentSize中,我們改變當(dāng)前cell的原點(diǎn)位置
//不加這個(gè)判斷的后果是微谓,UICollectionView只顯示一行森篷,原因是下面所有cell的x值都被加到第一行最后一個(gè)元素的后面了
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return attributes;
}
此時(shí),因?yàn)槭怯玫那耙粋€(gè)cell的最大X值加上0作為下一個(gè)cell的X值豺型,而最開始的X值已經(jīng)有誤差了仲智,最后累加的無車形成明顯的空隙,因此改為用 每個(gè)cell的寬 列索引* 作為X值触创;
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray* attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
//從第2個(gè)循環(huán)到最后一個(gè)
for(int i = 1; i < [attributes count]; ++i) {
UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];
UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[i - 1];
//設(shè)置最大間距坎藐,可根據(jù)需要改
NSInteger maximumSpacing = 0;
NSInteger maxX = CGRectGetMaxX(prevLayoutAttributes.frame);
int col_idx = i % 7;
float W = SCREEN_WIDTH/7;
//如果當(dāng)前一個(gè)cell的最右邊加上我們想要的間距加上當(dāng)前cell的寬度依然在contentSize中,我們改變當(dāng)前cell的原點(diǎn)位置
//不加這個(gè)判斷的后果是哼绑,UICollectionView只顯示一行岩馍,原因是下面所有cell的x值都被加到第一行最后一個(gè)元素的后面了
if(maxX + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = col_idx * W;
currentLayoutAttributes.frame = frame;
}
}
return attributes;
}