Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mangoweb/presentables
Simple reactive library for managing table views & collection views, written in Swift
https://github.com/mangoweb/presentables
Last synced: 14 days ago
JSON representation
Simple reactive library for managing table views & collection views, written in Swift
- Host: GitHub
- URL: https://github.com/mangoweb/presentables
- Owner: manGoweb
- License: mit
- Created: 2017-07-24T21:26:11.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-02-16T11:30:11.000Z (almost 4 years ago)
- Last Synced: 2024-10-28T12:54:58.150Z (23 days ago)
- Language: Swift
- Homepage:
- Size: 624 KB
- Stars: 12
- Watchers: 9
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Presentables
[![Slack](https://img.shields.io/badge/join-slack-745EAF.svg?style=flat)](http://bit.ly/2B0dEyt)
[![SPM compatible](https://img.shields.io/badge/SPM-compatible-4BC51D.svg?style=flat)](https://developer.apple.com/documentation/xcode/creating_a_standalone_swift_package_with_xcode)
[![License](https://img.shields.io/badge/license-MIT-745EAF.svg?style=flat)](https://github.com/manGoweb/Presentables/blob/master/LICENSE)
[![Platform](https://img.shields.io/badge/platform-iOS-745EAF.svg)](https://github.com/manGoweb/Presentables)##
So what is Presentables really? It is a really small library that should help you develop apps with table views a little bit faster than you would using the conventional methods. It also introduces binding of your data to the table views so any time you change your dataset, the table will update accordingly.
> Checkout our examples for **UICollectionView** support!
## Slack
Get help using and installing this product on our [Slack](http://bit.ly/2B0dEyt), channel #help-presentables
## Installation
#### Swift Package Manager (SPM)
```swift
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/manGoweb/Presentables.git", from: "1.0.0")
]
```## Usage
Create your cell like you would usually do
```Swift
import UIKitclass MyTableViewCell: UITableViewCell { }
```
There are two ways to use presentables
- [In a data manager](#using-data-manager)
- [In a view controller (or any other class) directly](#using-designated-view-controller)### Using data manager
Create a data manager
```Swift
import Foundation
import Presentablesclass TableDataManager: PresentableTableViewDataManager {
// MARK: Initialization
override init() {
super.init()
// Create a section
let section = PresentableSection()
// Create a cell with custom tap event
let presentable = Presentable.create({ (cell) in
cell.textLabel?.text = "First cell"
})
section.append(presentable)
// Now add your section to the data source
data.append(section)
}
}
```And lastly, bind your table view to your data manager
```Swift
import UIKit
import Presentablesclass ViewController: UITableViewController {
var manager: PresentableManager = TableDataManager()
// MARK: View lifecycleoverride func viewDidLoad() {
super.viewDidLoad()
tableView.bind(withPresentableManager: &manager)
}}
```or you could do a bit more ... here is a full spec data manager with all the functionality available
```Swift
import Foundation
import Presentablesclass TableDataManager: PresentableTableViewDataManager {
// MARK: Initialization
override init() {
super.init()
// Create a section in your table view
let section = PresentableSection()
// Add a header to it
let header = Presentable.create { (header) in
header.titleLabel.text = "It works :)"
}
section.header = header
// Create a cell with custom tap event
let presentable = Presentable.create({ (cell) in
cell.textLabel?.text = "First cell"
}).cellSelected {
print("First cell has been selected")
}
section.append(presentable)
// And add loads more different cells
for i in 2...51 {
let presentable = Presentable.create({ (cell) in
cell.textLabel?.text = "Id: \((i))"
})
section.append(presentable)
}
// Now add your section to the data source
data.append(section)
// And finally create a global tap event for all cells
selectedCell = { info in
info.tableView.deselectRow(at: info.indexPath, animated: true)
print("Did select cell no. \((info.indexPath.row + 1))")
}
}
// MARK: Overriding table view delegate (optional)
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
}
```### Using designated view controller
If you are not into creating separate manager classes, you can use our pre-set view controller directly like this:
```swift
class ManagerTableViewController: PresentableTableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Create a section
let section = PresentableSection()
// Build a header
let header = Presentable.create { (header) in
header.titleLabel.text = "It works yet again!"
}
section.header = header
// Add a cell!
let presentable = Presentable.create({ (cell) in
cell.textLabel?.text = "Custom cell"
})
section.append(presentable)
data.append(section)
}
}
```I don't think it can get simpler than that ...
In case you'd like to implement your own version of a `ManagerTableViewController` (or any other class or view on that matter), you could use `PresentableTableViewManageable` like this:
```swift
open class MyTableViewController: UIViewController, PresentableTableViewManageable {
public var tableView = UITableView(frame: CGRect.zero, style: .plain)
public let presentableManager = PresentableTableViewDataManager()
public var data: PresentableSections {
get { return presentableManager.data }
set { presentableManager.data = newValue }
}
// MARK: View lifecycle
open override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.snp.makeConstraints { ... }
bind()
}
}
```## Author
Ondrej Rafaj, [email protected]
## License
Presentables is available under the MIT license. See the LICENSE file for more info.