Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import UIKit
  2.  
  3. class GenericTableSource : NSObject {
  4.  
  5. private var cellID: String = ""
  6.  
  7. private (set) var data : [Any] = []
  8.  
  9. func registerCell(identifier: String, data : [String], tableView: UITableView) {
  10.  
  11. if identifier.isEmpty {
  12. fatalError("Tableview cell identifier can't be empty or nil")
  13. } else {
  14. self.cellID = identifier
  15. self.data = data
  16. tableView.register(UITableViewCell.self, forCellReuseIdentifier: self.cellID)
  17. }
  18. }
  19.  
  20. func cell(indexPath : IndexPath, tableview : UITableView)-> UITableViewCell {
  21. let cell = tableview.dequeueReusableCell(withIdentifier: self.cellID)
  22. cell?.textLabel?.text = self.data[indexPath.row] as? String
  23. return cell!
  24. }
  25.  
  26. }
  27.  
  28. //MARK: Tableview Delegate and DataSource
  29. extension GenericTableSource : UITableViewDelegate, UITableViewDataSource {
  30. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  31. return self.data.count
  32. }
  33.  
  34. func numberOfSections(in tableView: UITableView) -> Int {
  35. return 1
  36. }
  37.  
  38. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  39. return self.cell(indexPath: indexPath, tableview: tableView)
  40. }
  41. }
Add Comment
Please, Sign In to add comment