<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anastasiia on Medium]]></title>
        <description><![CDATA[Stories by Anastasiia on Medium]]></description>
        <link>https://medium.com/@StuAndSwift?source=rss-9c9fee299345------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Dmqg-BfrqJXsbsVaGNMFyw.jpeg</url>
            <title>Stories by Anastasiia on Medium</title>
            <link>https://medium.com/@StuAndSwift?source=rss-9c9fee299345------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 17:07:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@StuAndSwift/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Swift 5: UICollectionView with multiple cell types]]></title>
            <link>https://medium.com/@StuAndSwift/swift-5-uicollectionview-with-multiple-cell-types-33a2d28228c0?source=rss-9c9fee299345------2</link>
            <guid isPermaLink="false">https://medium.com/p/33a2d28228c0</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[collectionview]]></category>
            <category><![CDATA[snapkit]]></category>
            <category><![CDATA[collectionview-cell]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Anastasiia]]></dc:creator>
            <pubDate>Tue, 02 Aug 2022 09:46:41 GMT</pubDate>
            <atom:updated>2022-08-02T09:59:41.953Z</atom:updated>
            <content:encoded><![CDATA[<h3>Preface</h3><p>I’m not a brilliant designer. My task is to show how to use different cells on the same CollectionView. I have learned this way through my work. This is my first article, so please, if you have any comments, contact me. Thank you and have a nice reading ❤️</p><h3>Set up project</h3><p>The application consists of a single UICollectionViewController. The first cell is just a picture elongated in height, the second is a label, the third is a collection with pictures, the fourth (footer) is a label and two buttons.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*HMpmGgcTA9l56xTY4Rr9dw.gif" /><figcaption>iPod touch 7th generation</figcaption></figure><p>Before we start:</p><ol><li>The app will be created programmatically. Remove storyboard (<a href="https://medium.com/swift-productions/ios-start-a-project-without-storyboard-xcode-12-253d785af5e7">this</a> article will help you)</li><li>To work with constraints, I will use <a href="https://cocoapods.org/pods/SnapKit">SnapKit</a></li><li>To hide navigation bar in <em>viewWillAppear </em>write</li></ol><pre>navigationController?.setNavigationBarHidden(<strong>true</strong>, animated: animated)</pre><p>4. The images are saved in assets. For convenience, I will get access to the pictures through the extension UIImage (similarly for color):</p><pre><strong>extension</strong> UIImage {</pre><pre><strong> enum</strong> Photos {</pre><pre><strong>  static</strong> <strong>let</strong> sea = UIImage(named: &quot;sea&quot;)</pre><pre><strong>  static</strong> <strong>let</strong> bridge = UIImage(named: &quot;bridge&quot;)</pre><pre><strong>  static</strong> <strong>let</strong> dandelion = UIImage(named: &quot;dandelion&quot;)</pre><pre><strong>  static</strong> <strong>let</strong> mountain = UIImage(named: &quot;mountain&quot;)</pre><pre><strong>  static</strong> <strong>let</strong> killer_whale = UIImage(named: &quot;killer_whale&quot;)</pre><pre><strong>  static</strong> <strong>let</strong> polar = UIImage(named: &quot;polar&quot;)</pre><pre><strong>  static</strong> <strong>let</strong> river = UIImage(named: &quot;river&quot;)</pre><pre><strong>  static</strong> <strong>let</strong> winter = UIImage(named: &quot;winter&quot;)</pre><pre><strong>  static</strong> <strong>let</strong> forest_river = UIImage(named: &quot;forest_river&quot;)</pre><pre> }</pre><pre><strong> enum</strong> Icons {</pre><pre><strong>  static</strong> <strong>let</strong> tg_icon = UIImage(named: &quot;tg&quot;)</pre><pre><strong>  static</strong> <strong>let</strong> in_icon = UIImage(named: &quot;in&quot;)</pre><pre> }</pre><pre>}</pre><p>5. Some constants are stored in enum Constants:</p><pre><strong>enum</strong> Constants {</pre><pre><strong> static</strong> <strong>let</strong> height = UIScreen.main.bounds.height</pre><pre><strong> static</strong> <strong>let</strong> width = UIScreen.main.bounds.width</pre><pre><strong> static</strong> <strong>let</strong> photosCellWidth = width</pre><pre><strong> static</strong> <strong>let</strong> photosCellHeight = photosCellWidth / 1.5</pre><pre><strong> static</strong> <strong>let</strong> iconSize = width / 6</pre><pre>}</pre><h3>Write code</h3><h4>First view and CollectionViewController</h4><p>let’s create a view for the first collection cell. In this view , we will have only one UIImageView. During initialization, we will add imageView to the screen and set up constraints. It is important for the view to set the width equal to the width of the screen. ImageView is made according to the size of the entire view and set the height. We will also need the ID to register the future cell.View for the first cell will look like:</p><pre><strong>final</strong> <strong>class</strong> FirstView: UIView {<br> <strong>static</strong> <strong>let</strong> id = &quot;FirstView&quot;</pre><pre><strong> private</strong> <strong>lazy</strong> <strong>var</strong> imageView: UIImageView = {</pre><pre><strong>  let</strong> imageView = UIImageView()</pre><pre>  imageView.image = .Photos.mountain</pre><pre>  imageView.contentMode = .scaleToFill</pre><pre><strong>  return</strong> imageView</pre><pre> }()</pre><pre><strong> override</strong> <strong>init</strong>(frame: CGRect) {</pre><pre><strong>  super</strong>.init(frame: frame)</pre><pre>  setupView()</pre><pre> }</pre><pre><strong> public</strong> <strong>required</strong> <strong>init</strong>?(coder: NSCoder) {</pre><pre><strong>  super</strong>.init(coder: coder)</pre><pre>  setupView()</pre><pre> }</pre><pre><strong> private</strong> <strong>func</strong> setupView() {</pre><pre>  addSubview(imageView)</pre><pre>  setupConstraints()</pre><pre> }</pre><pre><strong> private</strong> <strong>func</strong> setupConstraints() {</pre><pre>  snp.makeConstraints { make <strong>in</strong></pre><pre>   make.width.equalTo(Constants.width)</pre><pre>  }</pre><pre>  imageView.snp.makeConstraints { make <strong>in</strong></pre><pre>   make.edges.equalToSuperview()</pre><pre>   make.height.equalTo(Constants.height * 0.7)</pre><pre>  }</pre><pre> }</pre><pre>}</pre><p>Before adding the view as a cell to the CollectionViewController, let’s configure the CollectionViewController. In <em>init </em>create layout and pass it in <em>super.init:</em></p><pre><strong>init</strong>() {</pre><pre><strong> let</strong> layout = UICollectionViewFlowLayout()</pre><pre> layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize</pre><pre><strong> super</strong>.init(collectionViewLayout: layout)</pre><pre>}</pre><p>UICollectionViewFlowLayout.automaticSize will automatically calculate the cell size depending on the content.</p><p>Now we need to create a cell that will contain FirstView (and all next views).</p><pre><strong>class</strong> CollectionViewCell: UICollectionViewCell {</pre><pre><strong> var</strong> view = UIView()</pre><pre><strong> func</strong> setupCell(view: UIView) {</pre><pre><strong>  self</strong>.view = view</pre><pre>  contentView.addSubview(view)</pre><pre>  view.snp.makeConstraints { make <strong>in</strong></pre><pre>  make.edges.equalToSuperview()</pre><pre>  }</pre><pre> }</pre><pre>}</pre><p>Now we have a cell that will adjust to the size of the content.</p><p>We do the registration in this way:</p><pre>collectionView.register(CollectionViewCell.<strong>self</strong>, forCellWithReuseIdentifier: FirstView.id)</pre><p>In <em>numberOfItemsInSection </em>return count of views (now it is 1) and in <em>cellForItemAt </em>put in cell the view:</p><pre><strong>public</strong> <strong>override</strong> <strong>func</strong> collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {</pre><pre><strong> guard</strong> <strong>let</strong> cell =     collectionView.dequeueReusableCell(withReuseIdentifier:  FirstView.id, for: indexPath) <strong>as</strong>? CollectionViewCell <strong>else</strong> { <strong>return</strong> UICollectionViewCell() }</pre><pre> cell.setupCell(view: FirstView())</pre><pre><strong> return</strong> cell</pre><pre>}</pre><p>We get (I have added a black background to the collectionView so that you can see the borders of the screen):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*hcbatC8dz-mqheF_O1nj_Q.png" /></figure><h4>Second view</h4><p>In the same way, create a second view with a label. Constraints in the view look like this:</p><pre>snp.makeConstraints { make <strong>in</strong></pre><pre> make.width.equalTo(Constants.width)</pre><pre>}</pre><pre>label.snp.makeConstraints { make <strong>in</strong></pre><pre> make.centerY.top.bottom.equalToSuperview()</pre><pre> make.leading.trailing.equalToSuperview().inset(15)</pre><pre>}</pre><p>For convenience in CollectionViewController add:</p><pre><strong>private</strong> <strong>lazy</strong> <strong>var</strong> firstView = FirstView()</pre><pre><strong>private</strong> <strong>lazy</strong> <strong>var</strong> secondView = SecondView()</pre><pre><strong>private</strong> <strong>lazy</strong> <strong>var</strong> cells: [(view: UIView, id: String)] = [</pre><pre>                         (firstView, FirstView.id),</pre><pre>                         (secondView, SecondView.id)]</pre><p>And <em>numberOfItemsInSection </em>and <em>cellForItemAt:</em></p><pre><strong> public</strong> <strong>override</strong> <strong>func</strong> collectionView(_ collectionView:  UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {</pre><pre> cells.count</pre><pre>}</pre><pre><strong>public</strong> <strong>override</strong> <strong>func</strong> collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {</pre><pre><strong> guard</strong> <strong>let</strong> cell =  collectionView.dequeueReusableCell(withReuseIdentifier:  cells[indexPath.row].id, for: indexPath) <strong>as</strong>? CollectionViewCell <strong>else</strong>  { <strong>return</strong> UICollectionViewCell() }</pre><pre> cell.setupCell(view: cells[indexPath.row].view)</pre><pre><strong> return</strong> cell</pre><pre>}</pre><p>And we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*vB6HoI57y-lQjJESAx7GmQ.png" /></figure><h4>Third view</h4><p>The third view will consist of a collection view. Set <em>scrollDirection </em>to <em>horizontal, </em>add <em>minimumLineSpacing </em>equal 3<em>. </em>We get:</p><pre><strong>final</strong> <strong>class</strong> ThirdView: UICollectionView {</pre><pre><strong> static</strong> <strong>let</strong> id = &quot;ThirdCell&quot;</pre><pre><strong> private</strong> <strong>let</strong> photos: [UIImage?] = [.Photos.sea,</pre><pre>.Photos.bridge,</pre><pre>.Photos.dandelion,</pre><pre>.Photos.killer_whale,</pre><pre>.Photos.polar,</pre><pre>.Photos.winter,</pre><pre>.Photos.river,</pre><pre>.Photos.forest_river]</pre><pre><strong> init</strong>() {</pre><pre><strong> let</strong> layout = UICollectionViewFlowLayout()</pre><pre> layout.scrollDirection = .horizontal</pre><pre> layout.minimumLineSpacing = 3</pre><pre><strong> super</strong>.init(frame: .zero, collectionViewLayout: layout)</pre><pre> setupView()</pre><pre>}</pre><pre><strong>required</strong> <strong>init</strong>?(coder: NSCoder) {</pre><pre> fatalError(&quot;init(coder:) has not been implemented&quot;)</pre><pre>}</pre><pre><strong>private</strong> <strong>func</strong> setupView() {</pre><pre> showsHorizontalScrollIndicator = <strong>false</strong></pre><pre> register(PhotosCell.<strong>self</strong>, forCellWithReuseIdentifier: PhotosCell.id)</pre><pre> dataSource = <strong>self</strong></pre><pre> delegate = <strong>self</strong></pre><pre> }</pre><pre>}</pre><pre><strong><br>extension</strong> ThirdView: UICollectionViewDataSource {</pre><pre><strong> public</strong> <strong>func</strong> collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {</pre><pre> photos.count</pre><pre>}</pre><pre><strong> public</strong> <strong>func</strong> collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {</pre><pre><strong> guard</strong> <strong>let</strong> cell = collectionView.dequeueReusableCell(withReuseIdentifier: PhotosCell.id, for: indexPath) <strong>as</strong>? PhotosCell <strong>else</strong> {</pre><pre><strong>  return</strong> UICollectionViewCell()</pre><pre> }</pre><pre> cell.addImage(image: photos[indexPath.row])</pre><pre><strong> return</strong> cell</pre><pre> }</pre><pre>}</pre><pre><strong>extension</strong> ThirdView: UICollectionViewDelegateFlowLayout {</pre><pre><strong> func</strong> collectionView(_ collectionView: UICollectionView, layout  collectionViewLayout: UICollectionViewLayout, sizeForItemAt   indexPath: IndexPath) -&gt; CGSize {</pre><pre> CGSize(width: Constants.photosCellWidth, height:    Constants.photosCellHeight)</pre><pre> }</pre><pre>}</pre><p>And PhotosCell:</p><pre><strong>final</strong> <strong>class</strong> PhotosCell: UICollectionViewCell {</pre><pre><strong> static</strong> <strong>let</strong> id = &quot;PhotosCell&quot;</pre><pre><strong> private</strong> <strong>lazy</strong> <strong>var</strong> imageView: UIImageView = {</pre><pre><strong>  let</strong> imageView = UIImageView()</pre><pre>  imageView.contentMode = .scaleToFill</pre><pre><strong>  return</strong> imageView</pre><pre>}()</pre><pre><strong> public</strong> <strong>override</strong> <strong>init</strong>(frame: CGRect) {</pre><pre><strong>  super</strong>.init(frame: frame)</pre><pre>  setupView()</pre><pre>}</pre><pre><strong> required</strong> <strong>init</strong>?(coder: NSCoder) {</pre><pre>  fatalError(&quot;init(coder:) has not been implemented&quot;)</pre><pre>}</pre><pre><strong> func</strong> addImage(image: UIImage?) {</pre><pre><strong>  guard</strong> <strong>let</strong> image = image <strong>else</strong> { <strong>return</strong> }</pre><pre>  imageView.image = image</pre><pre>}</pre><pre><strong> private</strong> <strong>func</strong> setupView() {</pre><pre>  addSubview(imageView)</pre><pre>  setupConstraints()</pre><pre>}</pre><pre><strong> private</strong> <strong>func</strong> setupConstraints() {</pre><pre>  imageView.snp.makeConstraints { make <strong>in </strong></pre><pre>  make.edges.equalToSuperview()</pre><pre>  }</pre><pre> }</pre><pre>}</pre><p>Result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*Ai_VT9HbQVIHpF9DL8-wqA.gif" /></figure><h4>Footer</h4><p>And finally: add a footer.</p><p>In the usual way, create a view, inherit from UICollectionReusableView and add it to the collectview as follows:</p><pre>collectionView.register(FooterCell.<strong>self</strong>, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: FooterCell.id)</pre><p>Set view for footer and set footer size:</p><pre><strong>extension</strong> CollectionViewController: UICollectionViewDelegateFlowLayout {</pre><pre><strong> override</strong> <strong>func</strong> collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -&gt; UICollectionReusableView {</pre><pre><strong>  switch</strong> kind {</pre><pre><strong>   case</strong> UICollectionView.elementKindSectionFooter:</pre><pre><strong>    let</strong> footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: FooterCell.id, for: indexPath)</pre><pre><strong>    return</strong> footerView</pre><pre><strong>   default</strong>:</pre><pre><strong>    return</strong> UICollectionReusableView()</pre><pre>   }</pre><pre> }</pre><pre><strong> func</strong> collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -&gt; CGSize {</pre><pre>  CGSize(width: UIScreen.main.bounds.width, height: 120)</pre><pre> }</pre><pre>}</pre><p>Return the background color to normal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*5v9AUWq-ts565GvmbxwgrQ.png" /></figure><p>That is it!</p><p>I hope this article helped you. You can find the complete project <a href="https://github.com/StuLolka/CustomCellCollectionView">here</a>. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33a2d28228c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>