美文网首页
补充笔记02-collection的简单使用

补充笔记02-collection的简单使用

作者: 没有北方的南方 | 来源:发表于2016-08-12 20:25 被阅读29次

    collection的简单使用

    import UIKit
    //与tableview极其相似,可以互相借鉴
    class ViewController: UIViewController,UICollectionViewDelegate, UICollectionViewDataSource {
        
        var collectionview : UICollectionView!
        override func viewDidLoad() {
            super.viewDidLoad()
            let layout = UICollectionViewFlowLayout()//开启自动布局
            layout.itemSize = CGSize(width: self.view.frame.width-20, height: 200)//应该先设置cell的大小
            layout.scrollDirection = .Horizontal//设置滑动的方向,也是cell依次摆放的顺序(下滑横放,右滑竖放)
            layout.minimumLineSpacing = 20//设置上下cell的最小间距(注意:设置的是最小间距,不能设定死的间距,会根据cell的大小摆放,下滑有效,左右滑动没有效果)
            layout.minimumInteritemSpacing = 20//设置左右cell的最小间距,当cell充满整个屏幕宽度时,设置无效
            layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)//设置section上下左右的间隙
            
            //设置collection的位置大小
            collectionview = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
            collectionview.dataSource = self//使collection关联类的代理
            collectionview.delegate = self
            collectionview.pagingEnabled = true//使滑动翻页
            collectionview.backgroundColor = UIColor.whiteColor()
            
            // 注册cell和header/footer
            collectionview.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")//注册cell
            self.view.addSubview(collectionview)
            
            collectionview.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
            collectionview.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer")
            
            //开启定时器
            NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(didTime(_:)), userInfo: nil, repeats: true)
        }
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
            //设置单双cell的颜色
            if indexPath.item % 2 == 0 {
                cell.contentView.backgroundColor = UIColor.redColor()
            }
            else {
                cell.contentView.backgroundColor = UIColor.blueColor()
            }
            return cell
        }
        //设置返回section的个数
        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 5
        }
        func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
            print("\(indexPath.row)\(indexPath.item)")
        }
        //    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //        return 4
        //    }//设置collection的个数
        
        //创建一个计时器,使页面实现自动切换
        func didTime(timer : NSTimer){
            let offset = collectionview.contentOffset
            //根据位置获取Cell(根据偏移量去找下一个cell,只要坐标在下一个cell视图中都可以)
            let indexPath = collectionview.indexPathForItemAtPoint(CGPoint(x: offset.x + 10, y: 20))
            //判断切换页面时下一个cell的下标
            if indexPath!.item == 3{
                collectionview.scrollToItemAtIndexPath(NSIndexPath(forItem: 0,inSection: 0), atScrollPosition: .Left, animated: false)
                
            }
            else{
                collectionview.scrollToItemAtIndexPath(NSIndexPath(forItem: indexPath!.item+3,inSection: 0), atScrollPosition: .Left, animated: true)
            }
        }
        
        //头尾的创建
    //        func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    //            if kind == UICollectionElementKindSectionHeader{
    //                let header = collectionview.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath)
    //                header.backgroundColor = UIColor.greenColor()
    //                return header
    //            }
    //            else{
    //    
    //                let footer = collectionview.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "footer", forIndexPath: indexPath)
    //                footer.backgroundColor = UIColor.grayColor()
    //                return footer
    //    
    //            }
    //        }
        //设置下一个cell的大小
    //        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    //            return CGSize(width: 10 * (indexPath.item + 1), height: 10 * (indexPath.item + 1))
    //        }
        //设置头尾的大小,宽高和排列方式有关
    //        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    //            return CGSize(width: 10, height: 200)
    //        }
    //        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    //            return CGSize(width: 100, height: 200)
    //        }
    }
    
    

    相关文章

      网友评论

          本文标题:补充笔记02-collection的简单使用

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