創(chuàng)建自定義的CollectionViewCell
import UIKit
class UserCustomCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {`
var collectionView:UICollectionView!
override funcviewDidLoad() {
super.viewDidLoad()
let layout =CustomLayout()
collectionView=UICollectionView(frame:CGRect(x:0, y:100, width:375, height:500), collectionViewLayout: layout)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "123")
collectionView.delegate = self
collectionView.dataSource = self
self.view.addSubview(collectionView)
}
// - MARK: - UICollectionDelegate
funccollectionView(_collectionView:UICollectionView, numberOfItemsInSection section:Int) ->Int{
return 10
}
funccollectionView(_collectionView:UICollectionView, cellForItemAt indexPath:IndexPath) ->UICollectionViewCell{
letcell = collectionView.dequeueReusableCell(withReuseIdentifier:"123", for: indexPath)
if indexPath.row%2==0{
cell.backgroundColor= colorLiteral(red: 0.8784313725, green: 0.07058823529, blue: 0.07058823529, alpha: 1)
}else{
cell.backgroundColor= colorLiteral(red: 0.01316650119, green: 0.1738643348, blue: 0.8993083835, alpha: 1)
}
return cell
}
}
class CustomLayout: UICollectionViewLayout {`
// 內(nèi)容區(qū)域總大小,不是可見(jiàn)區(qū)域
overridevarcollectionViewContentSize:CGSize{
let width = collectionView!.bounds.size.width - collectionView!.contentInset.left
-collectionView!.contentInset.right
letheight =CGFloat((collectionView!.numberOfItems(inSection:0) +1) /3)
* (width /3*2)
returnCGSize(width: width, height: height)
}
// 所有單元格位置屬性
overridefunclayoutAttributesForElements(in rect:CGRect)
-> [UICollectionViewLayoutAttributes]? {
varattributesArray = [UICollectionViewLayoutAttributes]()
letcellCount =self.collectionView!.numberOfItems(inSection:0)
foriin0..
letindexPath = IndexPath(item:i, section:0)
letattributes = self.layoutAttributesForItem(at: indexPath)
attributesArray.append(attributes!)
}
returnattributesArray
}
// 這個(gè)方法返回每個(gè)單元格的位置和大小
override funclayoutAttributesForItem(at indexPath:IndexPath)
->UICollectionViewLayoutAttributes? {
//當(dāng)前單元格布局屬性
let attribute = UICollectionViewLayoutAttributes(forCellWith:indexPath)
//單元格邊長(zhǎng)
let largeCellSide =collectionViewContentSize.width/3*2
let smallCellSide =collectionViewContentSize.width/3
//當(dāng)前行數(shù),每行顯示3個(gè)圖片,1大2小
let line:Int= indexPath.item/3
//當(dāng)前行的Y坐標(biāo)
letlineOriginY = largeCellSide *CGFloat(line)
//右側(cè)單元格X坐標(biāo),這里按左右對(duì)齊,所以中間空隙大
letrightLargeX =collectionViewContentSize.width- largeCellSide
letrightSmallX =collectionViewContentSize.width- smallCellSide
// 每行2個(gè)圖片,2行循環(huán)一次,一共6種位置
if (indexPath.item%6==0) {
attribute.frame=CGRect(x:0, y:lineOriginY, width:largeCellSide,
height:largeCellSide)
}else if(indexPath.item%6==1) {
attribute.frame=CGRect(x:rightSmallX, y:lineOriginY, width:smallCellSide,
height:smallCellSide)
}else if(indexPath.item%6==2) {
attribute.frame=CGRect(x:rightSmallX,
y:lineOriginY + smallCellSide,
width:smallCellSide, height:smallCellSide)
}elseif(indexPath.item%6==3) {
attribute.frame=CGRect(x:0, y:lineOriginY, width:smallCellSide,
height:smallCellSide )
}elseif(indexPath.item%6==4) {
attribute.frame=CGRect(x:0,
y:lineOriginY + smallCellSide,
width:smallCellSide, height:smallCellSide)
}else if(indexPath.item%6==5) {
attribute.frame=CGRect(x:rightLargeX, y:lineOriginY,
width:largeCellSide,
height:largeCellSide)
}
return attribute
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。