LTTableViewProtocal.swift
2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// LTTableViewProtocal.swift
// LTScrollView
//
// Created by 高刘通 on 2018/2/3.
// Copyright © 2018年 LT. All rights reserved.
//
import Foundation
import UIKit
public protocol LTTableViewProtocal { }
public extension LTTableViewProtocal {
private func configIdentifier(_ identifier: inout String) -> String {
var index = identifier.firstIndex(of: ".")
guard index != nil else { return identifier }
index = identifier.index(index!, offsetBy: 1)
identifier = String(identifier[index! ..< identifier.endIndex])
return identifier
}
func registerCell(_ tableView: UITableView, _ cellCls: AnyClass) {
var identifier = NSStringFromClass(cellCls)
identifier = configIdentifier(&identifier)
tableView.register(cellCls, forCellReuseIdentifier: identifier)
}
public func cellWithTableView<T: UITableViewCell>(_ tableView: UITableView) -> T {
var identifier = NSStringFromClass(T.self)
identifier = configIdentifier(&identifier)
var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
}
return cell as! T
}
public func tableViewConfig(_ delegate: UITableViewDelegate, _ dataSource: UITableViewDataSource, _ style: UITableView.Style?) -> UITableView {
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), style: style ?? .plain)
tableView.delegate = delegate
tableView.dataSource = dataSource
return tableView
}
public func tableViewConfig(_ frame: CGRect ,_ delegate: UITableViewDelegate, _ dataSource: UITableViewDataSource, _ style: UITableView.Style?) -> UITableView {
let tableView = UITableView(frame: frame, style: style ?? .plain)
tableView.delegate = delegate
tableView.dataSource = dataSource
return tableView
}
}