| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- //
- // BaseTableViewController.swift
- // MTP2_iOS
- //
- // Created by zhongyuan on 2018/4/11.
- // Copyright © 2018年 zhongyuan.All rights reserved.
- //
- import UIKit
- class BaseTableViewController<Model>: BaseViewController, UITableViewDataSource, UITableViewDelegate {
-
- var tableCellModels = [Model]() //要显示的数据
- var cellReuseIdentifier: String? //cell重用标识
-
- override func viewDidLoad() {
- super.viewDidLoad()
- }
-
- func numberOfSections(in tableView: UITableView) -> Int {
-
- return 1
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-
- return tableCellModels.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-
- assert(cellReuseIdentifier != nil, "请在子类中设置cellReuseIdentifier")
- //要求必须注册cell
- let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier!, for: indexPath) as! BaseTableViewCell<Model>
- cell.delegate = self
- cell.rowNum = indexPath.row
- cell.takeInfo = takeInfo
- cell.model = tableCellModels[indexPath.row]
- return cell
- }
-
- //注意,子类中需要用到的方法在这个类中都要定义,让子类覆盖的方式实现。否则,这个类没有实现的方法,子类不能覆盖,也不能继承!!!
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- tableView.deselectRow(at: indexPath, animated: true)
- }
-
- //定义已便让子类覆盖
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return UITableView.automaticDimension
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- return nil
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
-
- return 0
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
-
- return 0
- }
-
- func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
- return nil
- }
-
- func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
-
- }
-
- func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
-
- }
-
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
-
- }
-
- func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
-
- }
- }
- protocol GroupedTableViewDelegate {
-
- associatedtype Models
-
- var sections: Int { get }
- var rowsInSection: [Int] { get }
- subscript(row: Int, section: Int) -> Models { get set }
- }
- class GroupedTableViewController<Model>: BaseTableViewController<Model>, GroupedTableViewDelegate {
-
- lazy var rowsInSection = [Int](repeating: 0, count: sections-1)
-
- lazy var sections = 0
-
-
- typealias Models = Model
-
- func indexIsValid(row: Int, section: Int) -> Bool {
- return row >= 0 && row < rowsInSection[section] && section >= 0 && section < sections
- }
-
- func getRowAndSection(index: Int) -> (row: Int, section: Int)? {
-
- guard index < tableCellModels.count else { return nil }
-
- var indexs = 0
- for i in 0 ..< sections {
-
-
- if index < indexs + rowsInSection[i] {
-
- return (row: index - indexs, section: i)
- }
- indexs += rowsInSection[i]
- }
-
- return nil
- }
-
- func getModel(index: Int) -> Model? {
-
- guard index < tableCellModels.count - 1 else { return nil }
-
- var indexs = 0
- for i in 0 ..< sections - 1 {
-
-
- if index < indexs + rowsInSection[i] {
-
- return self[index - indexs, i]
- }
-
- indexs += rowsInSection[i]
- }
-
- return nil
- }
-
- subscript(row: Int, section: Int) -> Model {
- get {
-
- // assert(indexIsValid(row: row, section: section), "Index out of range")
- return getModel(row: row, section: section)
- }
- set {
-
- // assert(indexIsValid(row: row, section: section), "Index out of range")
- setModel(row: row, section: section, newModel: newValue)
- }
- }
-
- private final func getModel(row: Int, section: Int) -> Model {
-
- var rows = 0
-
- for i in 0..<section {
-
- rows += rowsInSection[i]
- }
-
- return tableCellModels[rows + row]
- }
-
- private final func setModel(row: Int, section: Int, newModel: Model) {
-
- var rows = 0
-
- for i in 0..<section {
-
- rows += rowsInSection[i]
- }
-
- tableCellModels[rows + row] = newModel
- }
-
- override func numberOfSections(in tableView: UITableView) -> Int {
-
- return sections
- }
-
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-
- return rowsInSection[section]
- }
-
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-
- assert(cellReuseIdentifier != nil, "请在子类中设置cellReuseIdentifier")
- //要求必须注册cell
- let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier!, for: indexPath) as! BaseTableViewCell<Model>
- cell.delegate = self
- cell.rowNum = indexPath.row
- cell.takeInfo = takeInfo
- cell.model = getModel(row: indexPath.row, section: indexPath.section)
- return cell
- }
- }
|