AliSoftware/Reusable

Consider adopting the responsibility of registering before dequeueing

nmdias opened this issue ยท 4 comments

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

Hi

Thanks for the suggestion!
I've seen this pattern used in some other projects too. I think it could be a great addition to Reusable indeed! Feel free to make a PR ๐Ÿ‘

djbe commented

I'd definitely add an option to disable the magic auto-register, I can imagine there's someone somewhere that might want to. Great idea though!

#63 ๐Ÿ‘

Hi,

To avoid the number of open issues / PRs to track, I'm closing this issue. But we can keep #63 open โ€“ I have to admit that I totally forgot about it and only remember it because I'm doing some triage tonight, but it's still a nice suggestion to keep in mind to add โ€“ and review if we merge #63 at some point.