加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift 中UICollection的一些简单的用法

发布时间:2020-12-14 01:52:07 所属栏目:百科 来源:网络整理
导读://1 viewDidLoad 里面 override func viewDidLoad() { super . viewDidLoad () self . view . backgroundColor = UIColor . whiteColor () let flowLayout = UICollectionViewFlowLayout () // 瀑布流 // 布局 collection var collection = UICollectionView

//1 viewDidLoad 里面

overridefunc viewDidLoad() {

super.viewDidLoad()

self.view.backgroundColor =UIColor.whiteColor()

let flowLayout =UICollectionViewFlowLayout()//瀑布流

//布局collection

var collection = UICollectionView(frame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height),collectionViewLayout: flowLayout)

collection.backgroundColor =UIColor.yellowColor()

//flowLayout.minimumLineSpacing = 10.0;//行间距(最小值)

//flowLayout.minimumInteritemSpacing = 50.0;//item间距(最小值)

collection.delegate =self

collection.dataSource =self

collection.registerClass(SCollectionViewCell.self,forCellWithReuseIdentifier:"cell")

//这个是关键要注册cell

self.view.addSubview(collection)

// Do any additional setup after loading the view.

}


//2 实现代理方法

func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath) -> UICollectionViewCell {

var cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell",forIndexPath: indexPath)

as!UICollectionViewCell

cell.backgroundColor =UIColor.redColor()

return cell

}

func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {

return 20 //返回cell的个数

}

func numberOfSectionsInCollectionView(collectionView:UICollectionView) -> Int{

return 1 //区间

}

//layout的布局

func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

if (indexPath.row %2 != 0 && indexPath.row %3 != 0) {

return CGSizeMake(140,70);//长的

}else{

return CGSizeMake(70,0)">方的

}

}

//两个cell的间隔

func collectionView(collectionView: UICollectionView,insetForSectionAtIndex section: Int) -> UIEdgeInsets {

return UIEdgeInsetsMake(5,5,5)

}

//选中cell

func collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath) {

print(indexPath.row)

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读