ishkawa / datasourcekit Goto Github PK
View Code? Open in Web Editor NEWDeclarative, testable data source of UICollectionView and UITableView.
License: MIT License
Declarative, testable data source of UICollectionView and UITableView.
License: MIT License
What is the preferred way to provide actions on cell?
I mean something like in https://github.com/maxsokolov/TableKit#row-actions>.
Thanks in advance.
Currently, declarations of contents of UITableView are expressed by [CellDeclaration]. One idea to support sections is expressing sections by some type like [TableViewSectionDeclaration<CellDeclaration>].
struct TableViewSectionDeclaration<CellDeclaration> {
let headerTitle: String?
let footerTitle: String?
let cellDeclarations: [CellDeclaration]
init(
headerTitle: String? = nil,
footerTitle: String? = nil,
declareCells: @escaping (@escaping (CellDeclaration) -> Void) -> Void) {
...
}
}
With TableViewSectionDeclaration<CellDeclaration>, a conformer may look like:
class ViewController: SectionsDeclarator {
enum CellDeclaration {
case a, b
}
func declareSections(_ section: (TableViewSectionDeclaration<CellDeclaration>) -> Void) {
section(TableViewSectionDeclaration(headerTitle: "A") { cell in
cell(.a)
})
section(TableViewSectionDeclaration(headerTitle: "B") { cell in
cell(.b)
cell(.b)
cell(.b)
})
}
}
I have a list view which is UIStackView added on UIScrollView and I want to use DataSourceKit with it.
I tried to define my own DataSource class for using with UIStackView, but CellBinder
's configureCell(_:)
is internal
so I gave up.
I like the concept of DataSourceKit and I think it is useful also for UIStackView and other kind of list view. Do you have a plan to support other than UITableView and UICollectionView?
Maybe I'm missing something but...
The dataSource declaration seems to be a straight forward mapping from enum value to cell type which always have the same structure. I think it's possible to move that mapping to the CellsDeclarator (and CellDeclaration) like the code shown below. You just have to make sure the makeBinder function is called when you call the cell function. This would make the code cleaner and even more compact.
enum CellDeclaration: Equatable {
case outline(BindableCell.Type, Venue)
case sectionHeader(BindableCell.Type, String)
case review(BindableCell.Type, Review)
case relatedVenue(BindableCell.Type, Venue)
}
struct Data: CellsDeclarator {
var venue: Venue
var reviews: [Review]
var relatedVenues: [Venue]
func declareCells(_ cell: (CellDeclaration) -> Void) {
cell(.outline(VenueOutlineCell.self, venue))
if !reviews.isEmpty {
cell(.sectionHeader(SectionHeaderCell.self, "Reviews"))
for review in reviews {
cell(.review(ReviewCell.self, review))
}
}
if !relatedVenues.isEmpty {
cell(.sectionHeader(SectionHeaderCell.self, "Related Venues"))
for relatedVenue in relatedVenues {
cell(.relatedVenue(RelatedVenueCell.self, relatedVenue))
}
}
}
}
Hi @ishkawa ,
I need your help, i'm trying to insert new data or cell, but when i run collectionView.reloadData(), nothing happen,
final class AdvancedVenueDetailViewController: UIViewController {
@IBOutlet private weak var collectionView: UICollectionView!
private let dataSource = CollectionViewDataSource<AdvancedVenueDetailViewState.CellDeclaration> { cellDeclaration in
switch cellDeclaration {
case .outline(let venue):
return VenueOutlineCell.makeBinder(value: venue)
case .sectionHeader(let title):
return SectionHeaderCell.makeBinder(value: title)
case .review(let review):
return ReviewCell.makeBinder(value: review)
case .relatedVenue(let venue):
return RelatedVenueCell.makeBinder(value: venue)
}
}
private var state = AdvancedVenueDetailViewState()
override func viewDidLoad() {
super.viewDidLoad()
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
collectionView.dataSource = dataSource
dataSource.cellDeclarations = state.cellDeclarations
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
self.state.reviews.append(Review(authorImage: #imageLiteral(resourceName: "Kaminarimon_at_night"), authorName: "Yosuke Ishikawa", body: "Lorem ipsum."))
self.collectionView.reloadData()
}
}
}
please help me :)
When opening the Xcode project (Xcode 10) Then It's empty. (no files or anything else)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.