Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CollectionView in UIView with xib file

i'm doing with this, i want to use CollectionView, but i haven't seen prototype cell, and don't know how to use CollectionView in this case, can someone help me ?

I try to use like this way but it take alot of time and hard to manage than UICollectionView

enter image description here

like image 317
Thanh Hải Avatar asked Dec 16 '16 19:12

Thanh Hải


People also ask

How do I add a collection view in Xib?

Set the cell's reusable identifier to CustomCell: Now back inside the CustomView we need to add collection view and register CustomCell. Open CustomView. xib and add UICollectionView, connect it with CustomView.

What is difference between Tableview and collectionView?

When would you choose to use a collection view rather than a table view? Suggested approach: Collection views are there to display grids, but also handle entirely custom layouts, whereas table views are simple linear lists with headers and footers.

How do I register collection view cell Xib in Swift?

let cell = collectionView. dequeueReusableCell(withReuseIdentifier: “collectionCell”, for: indexPath) in the place where you need the instance of the cell view. Then you can drag all the reference from storyboard to “yourCustomCell. swift” to do what ever you want for the view(etc.


1 Answers

The main way to use UICollectionView is by managing the logic programmatically.

  1. First, create a new class which inherits from UICollectionViewCell. Choose if you want to include a xib to easily design your cell: enter image description here

  2. Design your cell with Interface Builder or programmatically.

  3. Create your main view controller including a xib (or a storyboard) with the collection view inside and link it to the associated class via Interface Builder. Alternatively you can add a collection view programmatically to your UIViewController

enter image description here

  1. Make the target view controller conform to the UICollectionViewDelegate and UICollectionViewDataSource protocols by declaring them after the father class:

    class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
        @IBOutlet weak var collectionView: UICollectionView!
    
        //...
    } 
    
  2. Register the associated nib or the class for your cell in the viewDidLoad method and associate the datasource and delegate protocols to the view controller class:

     let cellIdentifier = "cellIdentifier"
    
     override func viewDidLoad() {
         super.viewDidLoad()
         //if you use xibs:
          self.collectionView.register(UINib(nibName:"MyCollectionCell", bundle: nil), forCellWithReuseIdentifier: cellIdentifier)
         //or if you use class:
          self.collectionView.register(MyCollectionCell.self, forCellWithReuseIdentifier: cellIdentifier)
    
          self.collectionView.delegate = self
          self.collectionView.dataSource = self
     }
    
  3. Implement the methods declared in the UICollectionViewDelegate and UICollectionViewDataSource protocols :

     let objects = ["Cat", "Dog", "Fish"]
    
     func numberOfSections(in collectionView: UICollectionView) -> Int {
           return 1
     }
    
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
           return self.objects.count
     }
    
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! MyCollectionCell
    
           //in this example I added a label named "title" into the MyCollectionCell class
           cell.title.text = self.objects[indexPath.item]
    
           return cell
     }
    
  4. Run your app in the simulator (or on a real device) and.. Et voilà! :)

enter image description here

For more info: https://developer.apple.com/reference/uikit/uicollectionview

like image 99
Nicola Giancecchi Avatar answered Sep 28 '22 04:09

Nicola Giancecchi