collectionView

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1. 系统默认提供布局方式: 多行多列布局
        let layout = UICollectionViewFlowLayout()
//        //2. 设置Cell的大小
        layout.itemSize = CGSize(width: self.view.bounds.size.width, height: 200)
//        //3. 滚动方向
        layout.scrollDirection = .Horizontal
        //设置上下左右的间距
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
//        //cell的最小间距
        layout.minimumInteritemSpacing = 0
//        //行间距
        layout.minimumLineSpacing = 0
        
        let rect = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height)
        collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.whiteColor()
        //显示滚动条
        collectionView.showsHorizontalScrollIndicator = false
        
        //分页显示
        collectionView.pagingEnabled = true
        
        //注册cell
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        //注册section header/footer
        collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
        collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer")
        
        collectionView.dataSource = self
        collectionView.delegate = self
        self.view.addSubview(collectionView)
        
        NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(didTimer(_:)), userInfo: nil, repeats: true)
    }
    
    func didTimer(timer: NSTimer) {
        //两种表示方法
        //NSIndexPath
        //位置: frame.origin
        //获取内容的偏移量
        let offset = collectionView.contentOffset
        //根据位置获取Cell
        let indexPath = collectionView.indexPathForItemAtPoint(CGPoint(x: offset.x, y: 0))
        if indexPath!.item == 4 {
            //滚动某个Cell到可见区域
            collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), atScrollPosition: .Left, animated: false)
            
            collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 1, inSection: 0), atScrollPosition: .Left, animated: true)
        }
        else {
            collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: indexPath!.item + 1, inSection: 0), atScrollPosition: .Left, animated: true)
        }
    }
    //设置Section的个数
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 4
    }

    //Section中Cell的个数
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        
        if indexPath.item % 2 == 0 {
            cell.contentView.backgroundColor = UIColor.redColor()
        }
        else {
            cell.contentView.backgroundColor = UIColor.blueColor()
        }
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("\(indexPath.section):\(indexPath.item)")
    }
    
    //section header
    //section footer
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        //header
        if kind == UICollectionElementKindSectionHeader {
            let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath)
            
            header.backgroundColor = UIColor.blueColor()
            return header
        }
        else {
            let footer = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "footer", forIndexPath: indexPath)
            
            footer.backgroundColor = UIColor.cyanColor()
            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: 100, height: 200)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 100, height: 200)
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容