美文网首页
瀑布流Swift

瀑布流Swift

作者: nihau | 来源:发表于2016-11-08 19:13 被阅读46次
Swift瀑布流

import UIKit

```

import SDWebImage

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

self.view.addSubview(CollectionView)

}

// MARK: - UICollectionView Lazy loading method

private lazy var CollectionView: UICollectionView = {

let lxLayOuy = LXLayOut()

lxLayOuy.delegate = self

let clv = UICollectionView(frame: UIScreen.mainScreen().bounds, collectionViewLayout:lxLayOuy)

clv.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: String(UICollectionViewCell))

clv.dataSource = self

clv.delegate = self

return clv

}()

private lazy var shops:[LXShop] = {

return LXShop.getShops()

}()

}

extension ViewController : UICollectionViewDataSource{

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{

return shops.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(String(UICollectionViewCell), forIndexPath: indexPath)

cell.backgroundColor = UIColor.redColor()

var img =  cell.viewWithTag(111) as? UIImageView

if img == nil {

img = UIImageView()

img?.tag = 111

cell.contentView.addSubview(img!)

}

let url = NSURL(string: shops[indexPath.item].img as! String)

img!.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)

img!.sd_setImageWithURL(url!, placeholderImage: UIImage(named: "5"), options: SDWebImageOptions.HighPriority)

return cell

}

}

extension ViewController : UICollectionViewDelegate{

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

print(indexPath)

}

}

// MARK: - LXLayOutDelegate method

extension ViewController : LXLayOutDelegate{

func getLayOutDatas(layOut: LXLayOut, width: CGFloat, index: Int) -> CGFloat {

let sp = shops[index]

return width * sp.h / sp.w

}

func getColumnMargin() -> CGFloat {

return 10

}

func getRowMargin() -> CGFloat {

return 10

}

func getColumnCount() -> Int {

return 2

}

func getEdgeInsets() -> UIEdgeInsets {

return  UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

}

}

这个是布局

import UIKit

import SDWebImage

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

self.view.addSubview(CollectionView)

}

// MARK: - UICollectionView Lazy loading method

private lazy var CollectionView: UICollectionView = {

let lxLayOuy = LXLayOut()

lxLayOuy.delegate = self

let clv = UICollectionView(frame: UIScreen.mainScreen().bounds, collectionViewLayout:lxLayOuy)

clv.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: String(UICollectionViewCell))

clv.dataSource = self

clv.delegate = self

return clv

}()

private lazy var shops:[LXShop] = {

return LXShop.getShops()

}()

}

extension ViewController : UICollectionViewDataSource{

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{

return shops.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(String(UICollectionViewCell), forIndexPath: indexPath)

cell.backgroundColor = UIColor.redColor()

var img =  cell.viewWithTag(111) as? UIImageView

if img == nil {

img = UIImageView()

img?.tag = 111

cell.contentView.addSubview(img!)

}

let url = NSURL(string: shops[indexPath.item].img as! String)

img!.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)

img!.sd_setImageWithURL(url!, placeholderImage: UIImage(named: "5"), options: SDWebImageOptions.HighPriority)

return cell

}

}

extension ViewController : UICollectionViewDelegate{

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

print(indexPath)

}

}

// MARK: - LXLayOutDelegate method

extension ViewController : LXLayOutDelegate{

func getLayOutDatas(layOut: LXLayOut, width: CGFloat, index: Int) -> CGFloat {

let sp = shops[index]

return width * sp.h / sp.w

}

func getColumnMargin() -> CGFloat {

return 10

}

func getRowMargin() -> CGFloat {

return 10

}

func getColumnCount() -> Int {

return 2

}

func getEdgeInsets() -> UIEdgeInsets {

return  UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

}

}

```

相关文章

  • 关于自定义瀑布流的布局(Swift)

    瀑布流 UICollectionView Swift 最近整理以前iOS开发中用到的功能代码,发觉对瀑布流的布局有...

  • swift 瀑布流

    一、创建WaterFallLayout,继承于UICollectionViewFlowLayout 二、创建属性 ...

  • 瀑布流Swift

    import UIKit ``` import SDWebImage class ViewController: ...

  • Swift - UICollectionView 瀑布流

    使用Swift实现简单的瀑布流,OC的实现和Swift大同小异,主要还是了解一下实现思路,我这里做了三种效果: 竖...

  • iOS Swift 瀑布流

    实现瀑布流布局的方式中利用“UICollectionView”与自定义“UICollectionViewLayou...

  • Swift 实现瀑布流

    引入三个变量 let wd =UIScreen.mainScreen().bounds.size.width //...

  • swift实现瀑布流

    类似瀑布流的使用场景很多. 实现瀑布流的关键节点: 实现cell的高度不同且没有规则的展示 这里整理实现瀑布流思路...

  • Swift-瀑布流

    import UIKit class SecondController: BaseViewController ,...

  • JMWaterFlow--快速布局瀑布流

    JMWaterFlow JMWaterFlow 是本人 Jimmy 使用Swift语言对常见的 瀑布流 布局做的封...

  • swift+CollectionView瀑布流

    记得在学习OC的时候,看到过一个CollectionView实现的瀑布流的项目,从网络加载图片,用SDWebIma...

网友评论

      本文标题:瀑布流Swift

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