Last active
December 11, 2019 09:44
-
-
Save 5SMNOONMS5/994e4b8c973139fc697c2c996eb9ab03 to your computer and use it in GitHub Desktop.
GenericType TableView, Copy and paste into your playground files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: A UIKit based Playground for presenting user interface | |
import UIKit | |
import PlaygroundSupport | |
/// CLSTableViewConfig for extend, add more tableview properties. | |
struct CLSTableViewConfig { | |
var heightForRow: CGFloat? | |
} | |
/// CLSTableView for single cell type only | |
final class CLSTableView<T, Cell: UITableViewCell>: UITableView, UITableViewDelegate, UITableViewDataSource { | |
var contents: [T] = [] { | |
didSet { | |
DispatchQueue.main.async { | |
self.reloadData() | |
} | |
} | |
} | |
var configure: (Cell, T) -> Void | |
var selectHandler: (T) -> Void | |
private let identifier = "identifier" | |
private var config: CLSTableViewConfig | |
/// It should be | |
init(contents: [T], | |
config: CLSTableViewConfig = CLSTableViewConfig(), | |
configure: @escaping (Cell, T) -> Void, | |
selectHandler: @escaping (T) -> Void) { | |
self.config = config | |
self.contents = contents | |
self.configure = configure | |
self.selectHandler = selectHandler | |
super.init(frame: .zero, style: .plain) | |
self.setup() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
private func setup() { | |
register(Cell.self, forCellReuseIdentifier: identifier) | |
/// It means automaticDimension | |
if let h = config.heightForRow { | |
rowHeight = h | |
} else { | |
estimatedRowHeight = 44.0 | |
rowHeight = UITableView.automaticDimension | |
} | |
super.delegate = self | |
super.dataSource = self | |
tableFooterView = UIView() | |
backgroundColor = .clear | |
separatorStyle = .none | |
} | |
/// Can't make an extensions of generic class in Swift4 for now | |
/// cf. https://stackoverflow.com/questions/48386613/make-an-extensions-of-generic-class-in-swift4 | |
// ****************************************** | |
// | |
// MARK: - UITableViewDelegate | |
// | |
// ****************************************** | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
tableView.deselectRow(at: indexPath, animated: true) | |
let content = contents[indexPath.row] | |
selectHandler(content) | |
} | |
// ****************************************** | |
// | |
// MARK: - UITableViewDataSource | |
// | |
// ****************************************** | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return contents.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! Cell | |
let content = contents[indexPath.row] | |
configure(cell, content) | |
return cell | |
} | |
} | |
final class MyViewController: UIViewController { | |
private let contents = | |
Array(repeating: "FFFFFFFF", count: 2) + | |
Array(repeating: "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG", count: 2) | |
/// CLSTableView | |
private lazy var tableView: CLSTableView<String, UITableViewCell> = { | |
return CLSTableView(contents: contents, configure: { (cell: UITableViewCell, item) in | |
cell.textLabel?.numberOfLines = 0 | |
cell.textLabel?.text = item | |
}, selectHandler: { (item) in | |
print("\(item) had been selected") | |
}) | |
}() | |
override func loadView() { | |
self.view = tableView | |
} | |
} | |
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 768, height: 1024)) | |
window.rootViewController = MyViewController() | |
window.makeKeyAndVisible() | |
PlaygroundPage.current.liveView = window | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment