giulio92 / GLTableCollectionView

Netflix and App Store like UITableView with UICollectionView, written in pure Swift 4.2
MIT License
705 stars 37 forks source link

Use custom UICollectionViewCell #34

Closed cylak closed 6 years ago

cylak commented 6 years ago

I want to use more than one UICollectionViewCell in the table view

Description:

I want to use more than one UICollectionViewCell in the table view but get fatal error like the custom cell didn't registered! It works just with one view cell (default cell or custom cell) but if i want to use mixed of these i got error. Thanks for your help 👍

Expected Behavior:

Show default cell with custom new cell

Actual Behavior:

Fatal error

Steps to Reproduce:

Custom cell -> GameCell.swift

import UIKit

class GameCell: UICollectionViewCell {

    static let id: String = "collectionViewCellID"

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    let imageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        iv.layer.cornerRadius = 16
        iv.layer.masksToBounds = true
        iv.image = UIImage(named: "someImage")
        return iv
    }()

    func setupViews() {
        addSubview(imageView)

        imageView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.width)
    }
}

GLCollectionTableViewCell.swift -> line 147

collectionView.register(GameCell.self, forCellWithReuseIdentifier: GameCell.id)

GLTableCollectionViewController.swift -> line 117

        if indexPath.item % 2 == 0 {
            guard let cell: GLIndexedCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: GLIndexedCollectionViewCell.identifier, for: indexPath) as? GLIndexedCollectionViewCell else {
                fatalError("UICollectionViewCell must be of GLIndexedCollectionViewCell type")
            }
            cell.backgroundColor = UIColor(hue: CGFloat(indexPath.item) / 20.0, saturation: 0.8, brightness: 0.9, alpha: 1)
            return cell
        }
        else{
            guard let cell: GameCell = collectionView.dequeueReusableCell(withReuseIdentifier: GameCell.id, for: indexPath) as? GameCell else {
                fatalError("UICollectionViewCell must be of GLIndexedCollectionViewCell type")
            }
            return cell
        }

Your Environment:

cylak commented 6 years ago

Sorry my bad, I found it!

GLTableCollectionViewController.swift -> line 117

        guard let indexedCollectionView: GLIndexedCollectionView = collectionView as? GLIndexedCollectionView else {
            fatalError("UICollectionView must be of GLIndexedCollectionView type")
        }
        if indexedCollectionView.indexPath.section % 2 == 0 {
            guard let cell: GLIndexedCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: GLIndexedCollectionViewCell.identifier, for: indexPath) as? GLIndexedCollectionViewCell else {
                fatalError("UICollectionViewCell must be of GLIndexedCollectionViewCell type")
            }
            cell.backgroundColor = UIColor(hue: CGFloat(indexPath.item) / 20.0, saturation: 0.8, brightness: 0.9, alpha: 1)
            return cell
        }
        else{
            guard let cell: GameCell = collectionView.dequeueReusableCell(withReuseIdentifier: GameCell.id, for: indexPath) as? GameCell else {
                fatalError("UICollectionViewCell must be of GLIndexedCollectionViewCell type")
            }
            return cell
        }