Skip to content

Instantly share code, notes, and snippets.

@FlorianBasso
Last active June 22, 2016 17:33
Show Gist options
  • Save FlorianBasso/e92914ecd92bbe0e1e1801f23d266dc1 to your computer and use it in GitHub Desktop.
Save FlorianBasso/e92914ecd92bbe0e1e1801f23d266dc1 to your computer and use it in GitHub Desktop.
Example of a basic ViewModel
protocol ViewModelDelegate: class {
func viewModelDidStartLoad()
func viewModelDidLoad()
func viewModelDidFail()
}
class ViewModel: NSObject {
// MARK: - Properties
private weak var delegate: ViewModelDelegate?
var items = [CellItem]()
// MARK: - Initialization
init(delegate: ViewModelDelegate) {
self.delegate = delegate
}
// MARK: - Configuration
func load() {
self.delegate?.viewModelDidStartLoad()
self.addItems()
self.delegate?.viewModelDidLoad()
}
}
// MARK: - UICollectionViewDataSource
extension ViewModel: UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let item = self.items[indexPath.row]
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(item.reuseIdentifier(), forIndexPath: indexPath)
item.configureCell(cell)
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment