Is completely static UICollectionView possible?

13,388

Solution 1

No.

Creating a static UICollectionViewController is not allowed. You must have a data source delegate.

I also want to point out that there is not a static UITableView, but a static UITableViewController. It's a difference.

Solution 2

You can easily create a static UICollectionViewController.

Just create every cell in interface builder, give them re-use identifiers(e.g. "Home_1" "Home_2" "Home_3"), and populate the methods as follows:

class HomeViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {  
    let cellIdentifiers:[String] = ["Home_1","Home_2","Home_3"]
    let sizes:[CGSize] = [CGSize(width:320, height:260),CGSize(width:320, height:160),CGSize(width:320, height:100)]

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellIdentifiers.count
    }
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifiers[indexPath.item], for: indexPath)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return sizes[indexPath.item]
    }
}

Then set the view controller to be of the proper class, and, hey presto, a (basically) static collection. I'm sorry to say but this is BY FAR the best way to support portrait and landscape views when you have groups of controls...

Share:
13,388

Related videos on Youtube

kmugitani
Author by

kmugitani

A programmer. iOS application and Linux.

Updated on June 16, 2022

Comments

  • kmugitani
    kmugitani almost 2 years

    At UITableView, completely static tableView config is possible. You can disconnect UITableView's datasource and put each cell on storyboard(or xib) by using IB.

    I tried same thing with UICollectionView. disconnect UICollectionView's datasource. Put each cell on UICollectionView on storyboard. I built it without any errors. But it didin't work. cells were not displayed at all.

    Is UICollectionView without datasource possible?

    • Kyle Clegg
      Kyle Clegg about 8 years
      When you build your cells in cellForItemAtIndexPath you could access a static array of cells rather than using dequeueReusableCellWithReuseIdentifier. I would only do this if you have a really strong use case for it though, as it's more likely to lead to bugs and performance issues.
  • kmugitani
    kmugitani about 9 years
    Ok. But what is your basis? Apple documents? Apple UIKit designer's response thay you got? Or your experience?
  • AShavit
    AShavit about 9 years
    Experience. I didn't look it up, but you can see in IB that unlike UITableViewController, the UICollection VC doesn't give you an option to use static collection. You can also see that adding more cells in the collection view is just adding cell formats.