美文网首页iOS-swift技术干货
Swift UICollectionView使用

Swift UICollectionView使用

作者: Miaoz0070 | 来源:发表于2016-12-30 15:56 被阅读1315次


瞌睡的表情

各位亲们之前说了的tableView的具体使用方法,下面来说下collectionView的具体使用直接上代码:

1.initcollectionView、设置代理方法

func initCollectionView() {

let defaultLayout = UICollectionViewFlowLayout()

defaultLayout.scrollDirection = UICollectionViewScrollDirection.vertical//设置垂直显示

defaultLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)//设置边距

defaultLayout.itemSize = CGSize(width: kScreenWidth/2, height: 50)

defaultLayout.minimumLineSpacing = 0.0 //每个相邻的layout的上下间隔

defaultLayout.minimumInteritemSpacing = 0.0 //每个相邻layout的左右间隔

defaultLayout.headerReferenceSize = CGSize(width: 0, height: 0)

defaultLayout.footerReferenceSize = CGSize(width: 0, height: 15)

collectionView = UICollectionView(frame: CGRect(x:0, y:0, width:kScreenWidth, height:kScreenHeight), collectionViewLayout: defaultLayout)

collectionView?.backgroundColor = UIColor.white

collectionView?.register(MoneyCollectionViewCell.self, forCellWithReuseIdentifier: cellMIdentifier)

collectionView?.dataSource = self

collectionView?.delegate = self

self.view.addSubview(collectionView!)

}

2.添加collection的delegate和dataSource

//分区个数

func numberOfSections(in collectionView: UICollectionView) -> Int {

return dataSource.count;

}

// 每个区的item个数

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

let sectionArray:NSArray =  dataSource.object(at: section) as! NSArray

return sectionArray.count;

}

//显示cell

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{

let cell:MoneyCollectionViewCell  = collectionView.dequeueReusableCell(withReuseIdentifier: cellMIdentifier, for: indexPath as IndexPath) as! MoneyCollectionViewCell

let sectionArray:NSArray = dataSource.object(at: indexPath.section) as! NSArray

let rowDic:NSDictionary = sectionArray.object(at: indexPath.row) as! NSDictionary

cell.rowDic = rowDic

return cell;

}

//点击item

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)

{

print("点击了第\(indexPath.section) 分区 ,第\(indexPath.row) 个元素")

}

//item的size

private func collectionView(collectionView: UICollectionView!,

layout collectionViewLayout: UICollectionViewLayout!,

sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {

return CGSize(width: kScreenWidth/2.0, height: 81)

}

func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, insetForSectionAtIndex section: Int) -> UIEdgeInsets{

return UIEdgeInsetsMake(0, 0, 0, 0);

}

//设置HeaderView的宽高

//MARK: UICollectionViewDelegateFlowLayout 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

return CGSize(width:collectionView.frame.size.width,height:35)

}

下边放出效果图

demo效果图

文中dataSource是数组数据,以上就是swift3.0 collectionView的简单使用,大家有什么不懂可以给我留言

完整代码地址点这里https://github.com/miaozhang9/Swift3.0Test_ImitateAliPay

相关文章

网友评论

    本文标题:Swift UICollectionView使用

    本文链接:https://www.haomeiwen.com/subject/mummvttx.html