This document discusses using protocols and view models to configure table view cells in Swift. It presents a problem with directly configuring cells and proposes using protocols to define cell configuration. Protocol extensions are used to provide default values. View models conform to these protocols to provide the data and behavior for configuring cells. This allows separating data, UI, and logic concerns to make the code more reusable, testable and maintainable.
14. protocol SwitchWithTextCellProtocol {
var title: String { get }
var titleFont: UIFont { get }
var titleColor: UIColor { get }
var switchOn: Bool { get }
var switchColor: UIColor { get }
func onSwitchTogleOn(on: Bool)
}
17. struct MinionModeViewModel: SwitchWithTextCellProtocol {
var title = "Minion Mode!!!"
var switchOn = true
var switchColor: UIColor {
return .yellowColor()
}
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
}
18. CELLFORROWATINDEXPATH
// YourViewController.swift
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell",
forIndexPath: indexPath) as! SwitchWithTextTableViewCell
// this is where the magic happens!
cell.configure(withDelegate: MinionModeViewModel())
return cell
20. protocol SwitchWithTextCellDataSource {
var title: String { get }
var switchOn: Bool { get }
}
protocol SwitchWithTextCellDelegate {
func onSwitchTogleOn(on: Bool)
var switchColor: UIColor { get }
var textColor: UIColor { get }
var font: UIFont { get }
}
23. extension MinionModeViewModel: SwitchWithTextCellDelegate {
var switchColor: UIColor {
return .yellowColor()
}
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
}
31. protocol TextPresentable {
var text: String { get }
var textColor: UIColor { get }
var font: UIFont { get }
}
protocol SwitchPresentable {
var switchOn: Bool { get }
var switchColor: UIColor { get }
func onSwitchTogleOn(on: Bool)
}
32. protocol ImagePresentable {
var imageName: String { get }
}
protocol TextFieldPresentable {
var placeholder: String { get }
var text: String { get }
func onTextFieldDidEndEditing(textField: UITextField)
}
33. extension TextPresentable {
var textColor: UIColor {
return .blackColor()
}
var font: UIFont {
return .systemFontOfSize(17)
}
}
34. !!!
class SwitchWithTextTableViewCell<T where T: TextPresentable, T: SwitchPresentable>: UITableViewCell {
private var delegate: T?
func configure(withDelegate delegate: T) {
// configure views here
}
}
35. extension MinionModeViewModel: TextPresentable {
var text: String { return "Minion Mode" }
var textColor: UIColor { return .blackColor() }
var font: UIFont { return .systemFontOfSize(17.0) }
}
36. extension MinionModeViewModel: SwitchPresentable {
var switchOn: Bool { return false }
var switchColor: UIColor { return .yellowColor() }
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
}
37. let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell",
forIndexPath: indexPath) as! SwitchWithTextTableViewCell<MinionModeViewModel>
let viewModel = MinionModeViewModel()
cell.configure(withDelegate: viewModel)
return cell