UIColelctionView Pinterest样式布局

时间:2018-03-07 23:56:00

标签: ios swift uicollectionview uicollectionviewlayout

我正在遵循一个pinterest风格的UICollectionViewLayout教程,但教程有一个照片模型,我的照片是URL格式,所以我无法在委托方法中有一个返回功能。如何返回我的图像以满足此要求。 image

extension PagesCollectionViewController : PinterestLayoutDelegate {
func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat {

    return userArray[indexPath.row].photoURL
    //   return photos[indexPath.item].image.size.height
}



   import Foundation
  import Firebase
  import FirebaseDatabase

 struct Users {
var email: String!
var uid: String!
var ref: DatabaseReference!
var location: String?
var photoURL: String!
var biography: String?
var key: String?
var name: String!
var phoneNumber: NSNumber?
var password: String!


init(snapshot: DataSnapshot) {

    if let snap = snapshot.value as? [String: Any]{
        self.password = snap["password"] as! String
        self.email = snap["email"] as! String
        self.uid = snap["uid"] as! String


        self.phoneNumber = Double(snap["phoneNumber"] as! String) as? NSNumber
        self.location = snap["location"] as! String
        self.photoURL = snap["photoURL"] as! String
        self.biography = snap["biography"] as! String
       self.name = snap["firstLastName"] as! String

    }

}

init(data: NSDictionary) {

    self.password = data.value(forKey: "password") as! String
    self.email = data.value(forKey: "email") as! String
    self.uid = data.value(forKey: "uid") as! String
    self.phoneNumber = Double(data.value(forKey: "phoneNumber") as! String) as? NSNumber
    self.location = data.value(forKey: "location") as! String
    self.photoURL = data.value(forKey: "photoURL") as! String
    self.biography = data.value(forKey: "biography") as! String
    self.name = data.value(forKey: "firstLastName") as! String
}

init(email: String, uid: String) {
    self.email = email
    self.uid = uid
    self.ref = Database.database().reference()
}
}

1 个答案:

答案 0 :(得分:0)

开放源代码项目,以实现具有不同图像和文本大小的Pinterest(如自定义集合视图布局)。

https://github.com/FarisAlbalawi/PinterestUISwift

enter image description here

安装

pod 'PinterestUISwift'

用法

    import PinterestUISwift

    class ViewController: UIViewController, collectionViewFlowDataSource{

        // MARK: Variables
        var collectionView: UICollectionView!

       override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

            let layout = collectionViewLayout(delegate: self)
            if #available(iOS 10.0, *) {
                layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
            } else {
                // Fallback on earlier versions
            }
            collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height:self.view.frame.height), collectionViewLayout: layout)

            collectionView.backgroundColor = .white
            collectionView.dataSource = self
            collectionView.delegate = self
            collectionView.showsHorizontalScrollIndicator = false
            collectionView.showsVerticalScrollIndicator = false
            view.addSubview(collectionView)

            collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")

            let nib:UINib = UINib(nibName: "Header", bundle: nil)
            collectionView.register(nib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "Header")



        }
    }




// collection View
extension ViewController: collectionViewFlowDataSource {

    func sizeOfItemAtIndexPath(at indexPath: IndexPath) -> CGFloat {
        let height = Float.random(in: 80 ..< 400)
        return CGFloat(height)
    }


    func numberOfCols(at section: Int) -> Int {
        return numberCols()

    }

    func spaceOfCells(at section: Int) -> CGFloat{
        return 12
    }

    func sectionInsets(at section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 50, right: 10)
    }

    func sizeOfHeader(at section: Int) -> CGSize{
        return CGSize(width: view.frame.width, height: 150)
    }

    func heightOfAdditionalContent(at indexPath : IndexPath) -> CGFloat{
        return 0
    }
}


// MARK: Data source
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

        return 40
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell


       // cell.layer.backgroundColor = RandomColor().cgColor
        cell.layer.cornerRadius = 8

        return cell
    }

    // Mark: header config
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        if kind == UICollectionView.elementKindSectionHeader {
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader,withReuseIdentifier:"Header", for: indexPath) as! Header



            return headerView
        } else {
            return UICollectionReusableView()
        }
    }


}