|
@@ -10,7 +10,13 @@ import UIKit
|
|
public protocol TSSimpleConfigurableView {
|
|
public protocol TSSimpleConfigurableView {
|
|
var data:Any? { get set }
|
|
var data:Any? { get set }
|
|
var delegate:TSSimpleCollectionViewDelegate? { get set }
|
|
var delegate:TSSimpleCollectionViewDelegate? { get set }
|
|
- var indexPath:IndexPath? { get set }
|
|
|
|
|
|
+ var indexPath:IndexPath { get set }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+public extension TSSimpleConfigurableView {
|
|
|
|
+ func actionCustom(custom:String){
|
|
|
|
+ delegate?.collectionView(didTrigger: TSSimpleCellEvent(action: .custom(custom), indexPath: indexPath, data: data))
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// 单元格事件类型
|
|
// 单元格事件类型
|
|
@@ -70,6 +76,8 @@ open class TSSimpleCollectionView: UIView {
|
|
cv.backgroundColor = .clear
|
|
cv.backgroundColor = .clear
|
|
cv.delegate = self
|
|
cv.delegate = self
|
|
cv.dataSource = self
|
|
cv.dataSource = self
|
|
|
|
+ cv.showsHorizontalScrollIndicator = false
|
|
|
|
+ cv.showsVerticalScrollIndicator = false
|
|
return cv
|
|
return cv
|
|
}()
|
|
}()
|
|
|
|
|
|
@@ -78,6 +86,13 @@ open class TSSimpleCollectionView: UIView {
|
|
|
|
|
|
public var cellTypes: [String: (UIView & TSSimpleConfigurableView).Type] = [:]
|
|
public var cellTypes: [String: (UIView & TSSimpleConfigurableView).Type] = [:]
|
|
public var cellIdentifierForItem: ((Any) -> String)?
|
|
public var cellIdentifierForItem: ((Any) -> String)?
|
|
|
|
+
|
|
|
|
+ public var headerTypes: [String: (UIView & TSSimpleConfigurableView).Type] = [:]
|
|
|
|
+ public var headerIdentifierForItem: ((Any) -> String)?
|
|
|
|
+
|
|
|
|
+ public var bottomTypes: [String: (UIView & TSSimpleConfigurableView).Type] = [:]
|
|
|
|
+ public var bottomIdentifierForItem: ((Any) -> String)?
|
|
|
|
+
|
|
// MARK: - Initialization
|
|
// MARK: - Initialization
|
|
override init(frame: CGRect) {
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
super.init(frame: frame)
|
|
@@ -107,12 +122,12 @@ open class TSSimpleCollectionView: UIView {
|
|
}
|
|
}
|
|
|
|
|
|
public func registerSectionHeader<Cell: UIView & TSSimpleConfigurableView>(_ cellType: Cell.Type, identifier: String) {
|
|
public func registerSectionHeader<Cell: UIView & TSSimpleConfigurableView>(_ cellType: Cell.Type, identifier: String) {
|
|
- cellTypes[identifier] = cellType
|
|
|
|
|
|
+ headerTypes[identifier] = cellType
|
|
collectionView.register(cellType, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier)
|
|
collectionView.register(cellType, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier)
|
|
}
|
|
}
|
|
|
|
|
|
public func registerSectionFooter<Cell: UIView & TSSimpleConfigurableView>(_ cellType: Cell.Type, identifier: String) {
|
|
public func registerSectionFooter<Cell: UIView & TSSimpleConfigurableView>(_ cellType: Cell.Type, identifier: String) {
|
|
- cellTypes[identifier] = cellType
|
|
|
|
|
|
+ bottomTypes[identifier] = cellType
|
|
collectionView.register(cellType, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier)
|
|
collectionView.register(cellType, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -169,14 +184,14 @@ extension TSSimpleCollectionView: UICollectionViewDataSource, UICollectionViewDe
|
|
|
|
|
|
public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
|
|
public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
|
|
let data = sections[indexPath.section]
|
|
let data = sections[indexPath.section]
|
|
- let identifier = cellIdentifierForItem?(data) ?? String(describing: type(of: data))
|
|
|
|
|
|
+ let identifier = headerIdentifierForItem?(data) ?? String(describing: type(of: data))
|
|
|
|
|
|
- guard let cellType = cellTypes[identifier] else {
|
|
|
|
|
|
+ guard let headerType = headerTypes[identifier] else {
|
|
fatalError("未注册对应数据类型的区间头尾: \(identifier)")
|
|
fatalError("未注册对应数据类型的区间头尾: \(identifier)")
|
|
}
|
|
}
|
|
|
|
|
|
let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath)
|
|
let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath)
|
|
- if var configurableCell = reusableView as? (UICollectionViewCell & TSSimpleConfigurableView) {
|
|
|
|
|
|
+ if var configurableCell = reusableView as? TSSimpleConfigurableView {
|
|
configurableCell.data = data
|
|
configurableCell.data = data
|
|
configurableCell.delegate = delegate
|
|
configurableCell.delegate = delegate
|
|
configurableCell.indexPath = indexPath
|
|
configurableCell.indexPath = indexPath
|