Skip to content

ahmedAlmasri/SNAdapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Using

Step 1: Import the SNAdapter module in swift.

import SNAdapter

TableView

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")]

}

More Example

CollectionView

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")]

}

More Example

Requirements

  • Swift 4.2+
  • Xcode 10.0+
  • iOS 11.0+

Installation

SNAdapter is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SNAdapter'

Author

ahmedAlmasri, [email protected]

License

SNAdapter is available under the MIT license. See the LICENSE file for more info.