UICollectionView swift2模版
2016-02-18 01:16
441 查看
class testViewController:BaseViewController,UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{ lazy var myCollectionView:UICollectionView = { let layout = UICollectionViewFlowLayout() layout.minimumLineSpacing = 1.5 //上下间隔 layout.minimumInteritemSpacing = 1 //左右间隔 let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout:layout) collectionView.backgroundColor = UIColor.whiteColor() return collectionView }() override func viewDidLoad() { super.viewDidLoad() //加载页面元素 self.addSubView() self.makeConstraints() self.myCollectionView.reloadData() } func addSubView(){ self.view.backgroundColor = UIColor(rgba: "#F1F1F1") self.myCollectionView.delegate = self self.myCollectionView.dataSource = self //注册header self.myCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "TESTHeader") self.myCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "TESTCell") self.view.addSubview(myCollectionView) } //增加约束 func makeConstraints(){ //Add Constraints myCollectionView.snp_makeConstraints{make in make.top.bottom.left.right.equalTo(self.view) } } //MARK: - CollectionView 代理方法 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { //分栏数量 return 10 } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //每个分栏中Cell的个数 return 2 } func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { var reuseView:UICollectionReusableView reuseView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "TESTHeader", forIndexPath: indexPath) reuseView.backgroundColor = UIColor.blackColor() return reuseView } ///cell内容 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { //返回复用的cell var cell = collectionView.dequeueReusableCellWithReuseIdentifier("TESTCell", forIndexPath: indexPath) if indexPath.row % 2 == 0 { cell.backgroundColor = UIColor.redColor() }else{ cell.backgroundColor = UIColor.blueColor() } return cell } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { //返回每个cell的大小 return CGSize(width: KMainScreenWidth, height:150) } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { var h:CGFloat = 15 return CGSizeMake(KMainScreenWidth,h) } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { //返回sectionview的大小 var h:CGFloat = 50 return CGSizeMake(KMainScreenWidth,h) } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{ return UIEdgeInsets(top:0, left: 0, bottom:5, right: 0) } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { //Cell点击事件 } }
相关文章推荐
- Swift开发iOS应用(1)列表的实现
- Swift - 设置UIView的背景色和背景图片
- swift_学习中遇到的错误
- swift调用第三方oc库
- Swift 中的闭包循环引用
- Swift 单例与懒加载
- swift 中cell 自适应label高度
- swift27个开元第三方库
- OC与Swift随机函数的区别
- Swift添加Preprocessor Macros
- ios开发中的Swift面向对象
- ios开发中的Swift面向对象
- 《从零开始学Swift》学习笔记(Day67)——Cocoa Touch设计模式及应用之MVC模式
- 《从零开始学Swift》学习笔记(Day67)——Cocoa Touch设计模式及应用之MVC模式
- swift初见
- swift storyboard 跳转的2 种方式
- swift 可选(optional)
- 常量和变量——swift学习笔记(1)
- 推荐几个swift学习网站
- Swift语法_函数类型