解決 UICollectionView的問題

1、解決 UICollectionView的Cell 之間不應有的分割線

借鑒地址

  • 項目中UICollectionViewCell的寬度為 屏幕寬度 / 7,且最小間距已設置為0 , 但是展示的cell間總有莫名其妙的分割線,解決方法:創(chuàng)建新類 繼承 UICollectionViewFlowLayout,重寫下面方法
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
    
    NSMutableArray* attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    
    //從第2個循環(huán)到最后一個
    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;
            frame.origin.x = origin + maximumSpacing;
            currentLayoutAttributes.frame = frame;
            
        }
        
    }
    return attributes;
    
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容