美文网首页
瀑布流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

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