swift中UICollectionView的简单使用

importUIKit

classHomeViewController:BaseViewController,UICollectionViewDataSource,UICollectionViewDelegate{

staticletreuseIdentifier ="UICollectionViewCell"

varcollectionView:UICollectionView?

overridefuncviewDidLoad() {

super.viewDidLoad()

letlayout =UICollectionViewFlowLayout()

layout.itemSize=CGSize(width:100, height:100)

layout.minimumLineSpacing=10

layout.minimumInteritemSpacing=10

self.collectionView=UICollectionView(frame:CGRect(x:0, y:0, width:ScreenWidth_crx, height:ScreenHeight_crx-64), collectionViewLayout: layout)

self.collectionView?.delegate=self;

self.collectionView?.dataSource=self

self.collectionView?.backgroundColor=UIColor.white

self.view.addSubview(self.collectionView!)

//self.collectionView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)

self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier:HomeViewController.reuseIdentifier)

}

//MARK:- UICollectionViewDataSource

funccollectionView(_collectionView:UICollectionView, numberOfItemsInSection section:Int) ->Int{

return20

}

funccollectionView(_collectionView:UICollectionView, cellForItemAt indexPath:IndexPath) ->UICollectionViewCell{

letcell = collectionView.dequeueReusableCell(withReuseIdentifier:HomeViewController.reuseIdentifier, for: indexPath)

cell.backgroundColor=UIColor.red

returncell

}

//MARK:冒号后添加注释说明-添加横线列如下

//FIXME:表示此处有bug或者要优化列如下

//TODO:-今天就先写到这吧睡觉明天继续

//#warning swift2.0中下面发放替换

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容