Guest User

Untitled

a guest
Feb 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. class DataSource<T>: NSObject, UITableViewDataSource {
  2. var items: [T] {
  3. didSet {
  4. self.tableView?.reloadData()
  5. }
  6. }
  7.  
  8. weak var tableView: UITableView?
  9. var fill: (UITableViewCell, T) -> UITableViewCell
  10. var identifier: String
  11.  
  12. init(fill tableView: UITableView, with items: [T], identifier: String, fill: @escaping (UITableViewCell, T) -> UITableViewCell) {
  13. self.items = items
  14. self.tableView = tableView
  15. self.identifier = identifier
  16. self.fill = fill
  17. }
  18.  
  19. func numberOfSections(in tableView: UITableView) -> Int {
  20. return 1
  21. }
  22.  
  23. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  24. return items.count
  25. }
  26.  
  27. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  28. var cell = tableView.dequeueReusableCell(withIdentifier: identifier)!
  29. cell = fill(cell, items[indexPath.row])
  30. return cell
  31. }
  32. }
Add Comment
Please, Sign In to add comment