Created
February 4, 2022 21:39
-
-
Save christianselig/ec66a9f90d7cb7eb319348eed5984a81 to your computer and use it in GitHub Desktop.
Example code, simply throw this into a view controller from a sample project.
This file contains hidden or 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
import UIKit | |
struct IceCream { | |
let title: String | |
let icon: UIImage | |
} | |
struct AppSettings { | |
static var fontSize = 17.0 | |
} | |
class IceCreamViewController: UIViewController, UITableViewDataSource { | |
let tableView = UITableView(frame: .zero, style: .insetGrouped) | |
let iceCreams: [IceCream] = { | |
return [ | |
IceCream(title: "Chocolate Brontosaurus", icon: UIImage(systemName: "tray.and.arrow.up.fill")!), | |
IceCream(title: "Vanilla Attack of the Jedi 2: Tree Battles on Mars", icon: UIImage(systemName: "command")!), | |
IceCream(title: "Banana", icon: UIImage(systemName: "tornado")!), | |
IceCream(title: "Strawberry", icon: UIImage(systemName: "music.mic")!), | |
IceCream(title: "Coffee", icon: UIImage(systemName: "brain")!) | |
] | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.register(IceCreamCell.self, forCellReuseIdentifier: IceCreamCell.id) | |
tableView.dataSource = self | |
tableView.frame = view.bounds | |
view.addSubview(tableView) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
// Mimic the user changing the app font size | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) { | |
AppSettings.fontSize = 35.0 | |
self.tableView.visibleCells.forEach { $0.setNeedsUpdateConfiguration() } | |
} | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return iceCreams.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: IceCreamCell.id, for: indexPath) as! IceCreamCell | |
cell.iceCream = iceCreams[indexPath.row] | |
cell.accessoryView = UISwitch() | |
return cell | |
} | |
} | |
class IceCreamCell: UITableViewCell { | |
static let id = "IceCreamCell" | |
var iceCream: IceCream? | |
override func updateConfiguration(using state: UICellConfigurationState) { | |
super.updateConfiguration(using: state) | |
guard let iceCream = iceCream else { return } | |
var newConfig = defaultContentConfiguration().updated(for: state) | |
newConfig.text = iceCream.title | |
newConfig.image = iceCream.icon | |
let font = UIFont.systemFont(ofSize: AppSettings.fontSize, weight: .regular) | |
newConfig.textProperties.font = font | |
newConfig.imageProperties.preferredSymbolConfiguration = UIImage.SymbolConfiguration(font: font, scale: .large) | |
contentConfiguration = newConfig | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment