2016-07-11 33 views
0

ich eine benutzerdefinierte Zelle wie diese machen wollen:Benutzerdefinierte Collection Layout-

enter image description here

Hier ist mein Code:

import UIKit 

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { 

    @IBOutlet var collectionView: UICollectionView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     collectionView.delegate = self 
     collectionView.dataSource = self 
    } 

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

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ImageCollectionViewCell", forIndexPath: indexPath) as! ImageCollectionViewCell 
     if indexPath.row == 0 { 
      cell.imgView.image = UIImage(named: "1") 
     } else if indexPath.row == 1 { 
      cell.imgView.image = UIImage(named: "2") 
     } else { 
      cell.imgView.image = UIImage(named: "3") 
     } 
     return cell 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     let screenWidth = UIScreen.mainScreen().bounds.width 
     if indexPath.row == 0 { 
      return CGSize(width: screenWidth/2, height: screenWidth) 
     } else if indexPath.row == 1 { 
      return CGSize(width: screenWidth/2, height: screenWidth/2 - 20) 
     } else { 
      return CGSize(width: screenWidth/2, height: screenWidth/2 - 20) 
     } 
    } 
} 

Gibt es eine falsch mit meinem Code?

Antwort

1

Obwohl ich nicht wirklich weiß, wie Sie Ihr Problem beheben können, kenne ich eine Bibliothek von Drittanbietern, die genau das tut, was Sie wollen. Check this out.

Sie können es verwenden, um Ihr gewünschtes Ergebnis zu erhalten, oder vielleicht in den Code schauen und sehen, wie es gemacht wird.

P.S. Der Code ist in Objective-C.

+0

Vielen Dank. Ich werde es prüfen. – Khuong