各位親們之前說了的tableView的具體使用方法,下面來說下collectionView的具體使用直接上代碼:
1.initcollectionView、設(shè)置代理方法
func initCollectionView() {
let defaultLayout = UICollectionViewFlowLayout()
defaultLayout.scrollDirection = UICollectionViewScrollDirection.vertical//設(shè)置垂直顯示
defaultLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)//設(shè)置邊距
defaultLayout.itemSize = CGSize(width: kScreenWidth/2, height: 50)
defaultLayout.minimumLineSpacing = 0.0 //每個(gè)相鄰的layout的上下間隔
defaultLayout.minimumInteritemSpacing = 0.0 //每個(gè)相鄰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
//分區(qū)個(gè)數(shù)
func numberOfSections(in collectionView: UICollectionView) -> Int {
return dataSource.count;
}
// 每個(gè)區(qū)的item個(gè)數(shù)
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;
}
//點(diǎn)擊item
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
print("點(diǎn)擊了第\(indexPath.section) 分區(qū) ,第\(indexPath.row) 個(gè)元素")
}
//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);
}
//設(shè)置HeaderView的寬高
//MARK: UICollectionViewDelegateFlowLayout?
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width:collectionView.frame.size.width,height:35)
}
下邊放出效果圖
文中dataSource是數(shù)組數(shù)據(jù),以上就是swift3.0 collectionView的簡(jiǎn)單使用,大家有什么不懂可以給我留言
完整代碼地址點(diǎn)這里https://github.com/miaozhang9/Swift3.0Test_ImitateAliPay