首页 > 代码库 > UICollectionViewDelegateFlowLayout 使用
UICollectionViewDelegateFlowLayout 使用
import UIKit //UICollectionViewLayout //itemSize属性 //设定全局的Cell尺寸,如果想要单独定义某个Cell的尺寸,可以使用下面方法: // - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath // //minimumLineSpacing属性 //设定全局的行间距,如果想要设定指定区内Cell的最小行距,可以使用下面方法: //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section // //minimumInteritemSpacing属性 //设定全局的Cell间距,如果想要设定指定区内Cell的最小间距,可以使用下面方法: //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section; // //scrollDirection属性 //设定滚动方向,有UICollectionViewScrollDirectionVertical和UICollectionViewScrollDirectionHorizontal两个值。 // //headerReferenceSize属性与footerReferenceSize属性 //设定页眉和页脚的全局尺寸,需要注意的是,根据滚动方向不同,header和footer的width和height中只有一个会起作用。如果要单独设置指定区内的页面和页脚尺寸,可以使用下面方法: //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section // //sectionInset属性 //设定全局的区内边距,如果想要设定指定区的内边距,可以使用下面方法: //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section; //因为UICollectionViewDelegateFlowLayout实际上是UICollectionViewDelegate的一个子协议,它继承了UICollectionViewDelegate,所以只需要在声明处写上UICollectionViewDelegateFlowLayout就行了。 class ViewController: UIViewController , UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout { let cellIdentity = "cellIdentity" let cellhead = "cellhead" let cellfoot = "cellfoot" let sectioncount = 1 let datas = ["cell1" , "cell2" , "cell3"] var collect:UICollectionView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.navigationItem.title = "image" self.view.backgroundColor = UIColor.whiteColor() let layout = UICollectionViewFlowLayout() collect = UICollectionView(frame: CGRectMake(10, 70, self.view.frame.size.width-20,self.view.frame.size.height-80) , collectionViewLayout: layout) collect.delegate = self collect.dataSource = self self.view.addSubview(collect) collect.backgroundColor = UIColor.whiteColor() collect.layer.borderWidth=1 collect.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentity) collect.registerClass(CollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: cellhead) collect.registerClass(CollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: cellfoot) } //datasource func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return sectioncount } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return datas.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentity, forIndexPath: indexPath) cell.layer.borderWidth=1 for v in cell.contentView.subviews{ v.removeFromSuperview() } let lab = UILabel(frame: CGRectMake(10 , 10 , cell.frame.size.width-20 , cell.frame.size.height-20)) lab.numberOfLines = 0 lab.text = "\(datas[indexPath.item])(\(indexPath.section)-\(indexPath.item))" cell.contentView.addSubview(lab) return cell } //页眉页脚 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSizeMake(250, 50) } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSizeMake(250, 50) } func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) ->UICollectionReusableView { var reusable:CollectionReusableView! = nil if kind == UICollectionElementKindSectionHeader { reusable = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: cellhead, forIndexPath: indexPath) as! CollectionReusableView reusable.labText(cellhead) }else if kind == UICollectionElementKindSectionFooter { reusable = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: cellfoot, forIndexPath: indexPath) as! CollectionReusableView reusable.labText(cellfoot) } reusable.layer.borderWidth=1 return reusable } //UICollectionViewDelegateFlowLayout //设定collectionView(指定区)的边距 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5) } //设定指定区内Cell的最小间距,也可以直接设置UICollectionViewFlowLayout的minimumInteritemSpacing属性 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{ return 10 } //设定指定区内Cell的最小行距,也可以直接设置UICollectionViewFlowLayout的minimumLineSpacing属性 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { return 10 } //设定指定Cell的尺寸 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSizeMake(200, 100) } //当指定indexPath处的item被选择时触发 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("didSelectItemAtIndexPath ++ \(indexPath)") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
UICollectionViewDelegateFlowLayout 使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。