//
// Template.swift
// TableTest
//
// Created by heqinglong on 16/3/17.
// Copyright © 2016年 heqinglong. All rights reserved.
//
import Foundation
import UIKit
protocol MarkCellProtocol {
func identifier()->String
func heightForModel(model:Any!)->CGFloat
func setupWithModel(model:Any!, cell:UITableViewCell, actionDelegate:MarkTableCallBacks?)
func Class()->AnyClass
//应该在这个函数所隐藏的那个Cell所使用的是当前这个CellProtocol
}
extension UITableView{
func setDD(dd:MarkTableViewDD.SpecialSctionSpecialCell){
self.dataSource = dd
self.
delegate = dd
}
func setDD(dd:MarkTableViewDD.Normal, cellProtocol:MarkCellProtocol, useNib:Bool =
false){
self.dataSource = dd.realSpecialDD
self.
delegate = dd.realSpecialDD
dd.helper = cellProtocol
if useNib ==
false {
self.registerClass(cellProtocol.Class(), forCellReuseIdentifier: cellProtocol.identifier())
}
else{
self.registerNib(UINib(nibName:cellProtocol.identifier(), bundle: nil),
forCellReuseIdentifier: cellProtocol.identifier())
}
}
func setDD(dd:MarkTableViewDD.SingleSection, cellProtocol:MarkCellProtocol, useNib:Bool =
false){
self.dataSource = dd.realSpecialDD
self.
delegate = dd.realSpecialDD
dd.helper = cellProtocol
if useNib ==
false {
self.registerClass(cellProtocol.Class(), forCellReuseIdentifier: cellProtocol.identifier())
}
else{
self.registerNib(UINib(nibName:cellProtocol.identifier(), bundle: nil),
forCellReuseIdentifier: cellProtocol.identifier())
}
}
}
protocol MarkTableViewFooterSectionProtocol{
func height()->CGFloat
func view(section:Int, model:AnyObject!, actionDelegate:MarkTableCallBacks?)->UIView?
}
protocol MarkTableViewHeaderSectionProtocol{
func height()->CGFloat
func view(section:Int, model:AnyObject!, actionDelegate:MarkTableCallBacks?)->UIView?
}
@objc protocol MarkTableCallBacks{
optional func didSelect(indexPath: NSIndexPath)
optional func doAction(action:String, model:AnyObject!, fromClass:AnyClass)
}
class MarkTableViewDD{
/// 常规的tableViewDD,需要外部设置footer和header的代理到这个对象本身.callback必须传递进来,但是可以是nil的
class Normal:NSObject {
private var realSpecialDD:SpecialSctionSpecialCell!
var sectionDatas:Array<(sectionData:AnyObject?, cellData:NSArray?)>{
set{
self.realSpecialDD.sectionDatas = []
newValue.forEach({
self.realSpecialDD.sectionDatas.append((sectionData: $0.sectionData,
headerHelper: self.headerHelper,
footerHelper: self.footerHelper,
cellHelper: self.helper,
cellData: $0.cellData))
})
}
get{
return self.realSpecialDD.sectionDatas.map{
return ($0.sectionData, $0.cellData)
}
}
}
private var helper:MarkCellProtocol!{
didSet{
for i
in 0
self.realSpecialDD.sectionDatas.count-1{
self.realSpecialDD.sectionDatas[i].cellHelper = helper
}
}
}
private var footerHelper:MarkTableViewFooterSectionProtocol?
private var headerHelper:MarkTableViewHeaderSectionProtocol?
init(callbackObj:MarkTableCallBacks!,
headerHelper:MarkTableViewHeaderSectionProtocol?,
footerHelper:MarkTableViewFooterSectionProtocol? ){
super.init()
self.realSpecialDD = SpecialSctionSpecialCell(callbackObj: callbackObj)
self.footerHelper = footerHelper
self.headerHelper = headerHelper
}
}
class SingleSection:NSObject {
private var realSpecialDD:SpecialSctionSpecialCell!
var cellDatas:NSArray? {
set{
self.realSpecialDD.sectionDatas[0].cellData = newValue
}
get{
return self.realSpecialDD.sectionDatas[0].cellData
}
}
init(callbackObj:MarkTableCallBacks!){
super.init()
self.realSpecialDD = SpecialSctionSpecialCell(callbackObj: callbackObj)
self.realSpecialDD.sectionDatas = [(sectionData:nil, headerHelper:nil, footerHelper:nil, cellHelper:nil, cellData:self.cellDatas)]
}
private var helper:MarkCellProtocol!{
didSet{
self.realSpecialDD.sectionDatas[0].cellHelper = helper
}
}
}
typealias FullModelType = (sectionData: AnyObject?,
headerHelper:MarkTableViewHeaderSectionProtocol?,
footerHelper:MarkTableViewFooterSectionProtocol?,
cellHelper:MarkCellProtocol?,
cellData:NSArray?)
class SpecialSctionSpecialCell:NSObject, UITableViewDataSource, UITableViewDelegate {
var sectionDatas:Array<FullModelType> = [(sectionData:nil, headerHelper:nil, footerHelper:nil, cellHelper:nil, [])]
init(callbackObj:MarkTableCallBacks!){
self.
delegate = callbackObj
}
private var
delegate:MarkTableCallBacks
@objc func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionDatas.count
}
//Cell implementation
@objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let dataAndHelper = sectionDatas[indexPath.section]
if let helper = dataAndHelper.cellHelper {
let cell = tableView.dequeueReusableCellWithIdentifier(helper.identifier(), forIndexPath: indexPath)
helper.setupWithModel(sectionDatas[indexPath.section].cellData?[indexPath.row], cell: cell, actionDelegate:
delegate)
return cell
}
else{
return UITableViewCell()
}
}
@objc func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated:
true)
if let select =
delegate.didSelect {
select(indexPath)
}
}
@objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if let d = sectionDatas[section].cellData{
return d.count
}
else{
return 0
}
}
@objc func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let dataAndHelper = sectionDatas[indexPath.section]
if let helper = dataAndHelper.cellHelper {
return helper.heightForModel(sectionDatas[indexPath.section].cellData?[indexPath.row])
}
else{
return 0
}
}
//Section implementation
@objc func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
let dataAndHelper = sectionDatas[section]
if let footerHelper = dataAndHelper.footerHelper {
return footerHelper.height()
}
else{
return 0
}
}
@objc func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let dataAndHelper = sectionDatas[section]
if let headerHelper = dataAndHelper.headerHelper {
return headerHelper.height()
}
else{
return 0
}
}
@objc func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let dataAndHelper = sectionDatas[section]
if let footerHelper = dataAndHelper.footerHelper {
return footerHelper.view(section, model: sectionDatas[section].sectionData, actionDelegate:
delegate)
}
else{
return nil
}
}
@objc func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let dataAndHelper = sectionDatas[section]
if let headerHelper = dataAndHelper.headerHelper {
return headerHelper.view(section, model:sectionDatas[section].sectionData, actionDelegate:
delegate)
}
else{
return nil
}
}
}
}