ra1028/Carbon

How to set section titles for UITableView?

onmyway133 opened this issue · 3 comments

Hi, congrats on the new tool. I have a question, in UITableView, I can use titleForHeaderInSection, how to do that with Carbon?

Hello @onmyway133 ,
Thank you for always nice blogs and tools.

Carbon is implemented with no excessive wrapping for UIKit, so you can use delegate and dataSource methods in the same way as vanilla implementation by making the class inheriting UITableViewAdapter .
It's one of the valued concepts of Carbon.
The sample code for it is below.

final class CustomAdapter: UITableViewAdapter {
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Header title for section\(section)"
    }
}

final class YourViewController: UIViewController {
    private let tableView: UITableView = ...
    private lazy var renderer = Renderer(
        target: tableView,
        adapter: CustomAdapter(),
        updater: UITableViewUpdater()
    )
}

@ra1028 oh that's nice. I also played around with that same approach in Upstream https://github.com/onmyway133/Upstream last year. I think the ability to allow subclassing like you do is great, so users won't be restricted by the framework 👍

I've always intrigued in how to wrap around data source https://github.com/onmyway133/fantastic-ios-architecture#data-source, and yours seem s to be the most elegant solution

@onmyway133

Thanks.
Your framework Upstream(congrats!) is also interesting, and its approach is similar to Carbon.
They are few framework that actually declarative in the many frameworks that wrap around data source.
Also, thanks for listing.
Keep up the great work 👍