OOTableViewController.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // OOTableViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/4/8.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. public protocol Configurable {
  10. func config(withItem item: Any?)
  11. }
  12. open class OOTableViewController<T,Cell:UITableViewCell>:UITableViewController where Cell:Configurable {
  13. private let cellIdentifier = String(describing: Cell.self)
  14. var data = [T]() {
  15. didSet {
  16. tableView.reloadData()
  17. if tableView.numberOfRows(inSection: 0) > 0 {
  18. tableView.scrollToRow(at: IndexPath.init(row: 0, section: 0) , at: .top, animated: true)
  19. }
  20. }
  21. }
  22. init() {
  23. super.init(nibName:nil,bundle:nil)
  24. }
  25. required public init?(coder aDecoder: NSCoder) {
  26. fatalError("init(coder:) has not been implemented")
  27. }
  28. open override func viewWillAppear(_ animated: Bool) {
  29. super.viewWillAppear(true)
  30. navigationController?.navigationBar.isTranslucent = false
  31. }
  32. override open func viewDidLoad() {
  33. super.viewDidLoad()
  34. tableView.register(Cell.self, forCellReuseIdentifier: cellIdentifier)
  35. tableView.rowHeight = UITableView.automaticDimension
  36. tableView.estimatedRowHeight = 60
  37. }
  38. override open func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) -> Int {
  39. return data.count
  40. }
  41. override open func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) -> UITableViewCell {
  42. let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! Cell
  43. cell.config(withItem: data[indexPath.row])
  44. return cell
  45. }
  46. }