GithubHelp home page GithubHelp logo

collectionviewswift's Introduction

CollectionViewSwift

class ViewController: UIViewController,WaterFallLayoutDelegate,UICollectionViewDelegate,UICollectionViewDataSource{
    
    @IBOutlet weak var collection: UICollectionView!
    
    var flag:NSString = "NO"
    
    var cellCount = 10
    private lazy var cellHeight:[CGFloat] = {
        var array:[CGFloat] = []
        for _ in 0..<self.cellCount{
            array.append(CGFloat(arc4random()%150 + 40))
        }
        print(array);
        return array
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = UIColor.whiteColor()
        //线性
        //setLineLayout()
        //瀑布流
        setWaterFallLayout()
        //圆型
        //setCirCleLayout()
    }
    
    func setLineLayout(){
        let layout = LineLayout()
        layout.itemSize = CGSize(width: 100.0,height:100.0)
        collection?.collectionViewLayout = layout
        collection?.backgroundColor = UIColor.blueColor()
    }
    
    func setWaterFallLayout(){
        let layout = WaterFallLayout()
        layout.delegate = self
        layout.numberOfColums = 5
        collection?.collectionViewLayout = layout
        collection?.backgroundColor = UIColor.blackColor()
        flag = "YES"
    }
    
    func setCirCleLayout() -> Void {
        let layout = CirCleLayout()
        collection?.collectionViewLayout = layout
        collection?.backgroundColor = UIColor.yellowColor()
    }
    
    func heightForItemAtIndexPath(indexPath: NSIndexPath) -> CGFloat {
        return cellHeight[indexPath.row]
    }
   
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellCount
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellIdentifier", forIndexPath: indexPath)
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row % 2 == 0 {
            //偶数
            cellCount -= 1
            if flag.isEqual("YES") {
                self.cellHeight.removeAtIndex(indexPath.row)
            }
            collectionView.performBatchUpdates({
                collectionView.deleteItemsAtIndexPaths([indexPath])
                }, completion:nil)
        }else{
            cellCount += 1
            if flag.isEqual("YES") {
                self.cellHeight.append(CGFloat(arc4random() % 150 + 40))
            }
            collectionView.performBatchUpdates({
                collectionView.insertItemsAtIndexPaths([indexPath])
                }, completion: nil)
        }
    }

}

###感谢关注

有任何建议或想法请加QQ:578545715,你的点赞是我前行最大动力~!

###效果图 image image image

collectionviewswift's People

Contributors

leefenghy avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.