DenTelezhkin/DTCollectionViewManager

possible to have multiple model-types for a cell?

skydivedan opened this issue · 1 comments

I'm wondering if there is a way to use different model-types for a collection-view cell.

I have two types: Book and Movie. Each type has a title, and an associated image.

What I'd like to do is use the same UICollectionViewCell (let's call it GenericCell) and register it so that a different type can be used, besides the one that is defined in the cell's ModelType.

Barring that, maybe a runtime transform?... so I could see "Oh the model is a Book, so transform it to a ImageTitle object". Then the ModelType and update routine would use ImageTitle even though the object from the data-source was actually a Book or Movie or whatever.

Hi!

In this case, i would suggest you use a protocol to define common properties, for example

protocol Content {
    var title: String { get }
}

extension Book: Content {}
extension Movie: Content {}

Another option is to create enum with state:

enum Content {
    case book(Book)
    case movie(Movie)

    var title: String {
        switch self {
            case .book(let book): return book.title
            case .movie(let movie): return movie.title
        }
    }
}

In both cases, conformance to ModelTransfer protocol would look like:

class GenericCell: UICollectionViewCell, ModelTransfer {
    func update(with model: Content) {
        titleLabel.text = model.title
    }
}

Currently, there are no plans to support multi-model cell types. Hope two provided options suit your task.