// // BaseTableViewController.swift // MTP2_iOS // // Created by zhongyuan on 2018/4/11. // Copyright © 2018年 zhongyuan.All rights reserved. // import UIKit class BaseTableViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate { /// 要显示的数据 var tableCellModels = [Model]() /// cell重用标识 var cellReuseIdentifier: String? 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 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: BaseTableViewController, 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 { return getModel(row: row, section: section) } set { setModel(row: row, section: section, newModel: newValue) } } private final func getModel(row: Int, section: Int) -> Model { var rows = 0 for i in 0..
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 cell.rowNum = indexPath.row cell.takeInfo = takeInfo cell.model = getModel(row: indexPath.row, section: indexPath.section) return cell } }