A declarative type-safe framework for building fast and flexible lists with TableView & CollectionView, No more delegates and datasource. Just a fully type-safe declarative content approach
To run the example project, clone the repo, and run pod install
from the Example directory first.
Step 1: Import the SNAdapter
module in swift.
import SNAdapter
Step 1: declare a new class or struct conform from SNCellableModel
struct MyModel: SNCellableModel {
let title: String
}
Step 2: declare a new UITableViewCell
conform from SNCellable
class MyCell: UITableViewCell, SNCellable {
func configure(_ object: SNCellableModel?) {
guard let myModel = object as? MyModel else { return }
self.textLabel?.text = myModel.title
}
}
Step 3: Take a reference to SNTableViewSection
and SNTableViewAdapter
into your controller.
private var mySection: SNTableViewSection<MyModel, MyCell>!
private var myAdapter: SNTableViewAdapter!
Step 4: Initialize the SNTableViewSection
and SNTableViewAdapter
in your viewDidLoad .
override func viewDidLoad() {
super.viewDidLoad()
///....
// MARK: - call setup section
setupMySection()
///...
}
private func setupMySection() {
mySection = SNTableViewSection<MyModel, MyCell>(items: getMyListData())
myAdapter = SNTableViewAdapter(sections: [mySection])
myTableView.setAdapter(myAdapter)
}
private func getMyListData() -> [MyModel] {
return [MyModel(title: "Item 1"), MyModel(title: "Item 2")]
}
Step 1: declare new UICollectionViewCell
inherent form SNCellable
class MyCell: UICollectionViewCell, SNCellable {
@IBOutlet weak var titleLabel: UILabel!
func configure(_ object: SNCellableModel?) {
guard let myModel = object as? MyModel else { return }
titleLabel.text = myModel.title
}
}
Step 2: Take a reference to SNCollectionViewSection
and SNCollectionViewAdapter
into your controller.
private var mySection: SNCollectionViewSection<MyModel, MyCell>!
private var myAdapter: SNCollectionViewAdapter!
Step 3: Initialize the SNCollectionViewSection
and SNCollectionViewAdapter
in your viewDidLoad .
override func viewDidLoad() {
super.viewDidLoad()
///....
// MARK: - call setup section
setupMySection()
///...
}
private func setupMySection() {
mySection = SNCollectionViewSection<MyModel, MyCell>(items: getMyListData())
myAdapter = SNCollectionViewAdapter(sections: [mySection])
myCollectionView.setAdapter(myAdapter)
}
private func getMyListData() -> [MyModel] {
return [MyModel(title: "Item 1"), MyModel(title: "Item 2")]
}
- Swift 4.2+
- Xcode 10.0+
- iOS 11.0+
SNAdapter is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SNAdapter'
ahmedAlmasri, ahmed.almasri@ymail.com
SNAdapter is available under the MIT license. See the LICENSE file for more info.