JMWaterFlow--快速布局瀑布流

2015-12-14  本文已影响173人  Jimmy_P

JMWaterFlow

JMWaterFlow 是本人 Jimmy 使用Swift语言对常见的 瀑布流 布局做的封装。 它使用UICollectionView来布局瀑布流,通过自定义layout布局来实现,简单易用。

Github地址:

https://github.com/JimmyPeng4iOS/JMWaterFlow


使用方式

    let layout = WaterFlowViewLayout()
   //构造方法
   init()
   {
       super.init(collectionViewLayout: layout)
   } 
        let Margin:CGFloat = 8;
        
        /// 瀑布流四周的间距
        layout.sectionInsert = UIEdgeInsets(top: Margin, left: Margin, bottom: Margin, right: Margin)
        
        /// 瀑布流列数
        layout.column = 5
        
        /// 列间距
        layout.columnMargin = Margin
        
        /// 行间距
        layout.rowMargin = Margin
        
        // 设置代理
        layout.delegate = self
//MARK: 代理方法
    /// 返回每个cell的高度,需要heightForWidth等比例缩放后的
    func waterFlowViewLayout(waterFlowViewLayout: WaterFlowViewLayout, heightForWidth: CGFloat, atIndexPath: NSIndexPath) -> CGFloat
    {
        return CGFloat(100 + arc4random_uniform(50))//测试使用随机值
    }

    ///返回cell的数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 500//测试
    }
    
    //返回cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath)
        cell.backgroundColor = UIColor.randomColor()//测试使用随机色
        return cell
    }

最后 再来一遍github地址 (๑•̀ㅂ•́)و✧

https://github.com/JimmyPeng4iOS/JMWaterFlow


上一篇 下一篇

猜你喜欢

热点阅读