Skip to content

Consider adopting the responsibility of registering before dequeueing #51

@nmdias

Description

@nmdias

Hi,

As the titles says. I've been using this approach for some time:

public extension UITableView {
    /// Registers and dequeues a `Reusable` `UITableViewCell`.
    ///
    /// - Returns: A reusable cell.
    final func reusableCell<T: UITableViewCell>() -> T where T : Reusable {
        guard let cell = self.dequeueReusableCell(withIdentifier: T.reuseIdentifier) as? T else {
            self.register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
            return self.dequeueReusableCell(withIdentifier: T.reuseIdentifier) as! T
        }
        return cell
    }
    
}

Then:

let cell = tableView.reusableCell() as TableViewCell

No need to remind myself to register beforehand. I know the signatures don't match. It's just to get an idea.

Is there some edge case I'm not seeing where this might not be possible/advisable?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions