Last active
June 22, 2016 17:33
-
-
Save FlorianBasso/e92914ecd92bbe0e1e1801f23d266dc1 to your computer and use it in GitHub Desktop.
Example of a basic ViewModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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