Skip to content

Instantly share code, notes, and snippets.

@bgayman
Created January 23, 2018 20:03
Show Gist options
  • Save bgayman/3b17f749a5b082a93fd5844b7d0089ef to your computer and use it in GitHub Desktop.
Save bgayman/3b17f749a5b082a93fd5844b7d0089ef to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
final class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(TableViewCell.self, forCellReuseIdentifier: "\(TableViewCell.self)")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
perform(#selector(self.toggleExpanded), with: nil, afterDelay: 1.0)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "\(TableViewCell.self)", for: indexPath)
cell.backgroundColor = .blue
return cell
}
@objc
private func toggleExpanded() {
tableView.beginUpdates()
tableView.visibleCells.flatMap { $0 as? TableViewCell }.forEach { $0.isExpanded = !$0.isExpanded }
tableView.endUpdates()
perform(#selector(self.toggleExpanded), with: nil, afterDelay: 2.0)
}
}
final class TableViewCell: UITableViewCell {
var isExpanded: Bool = false {
didSet {
heightConstraint.constant = isExpanded ? 150 : 75
}
}
lazy var heightConstraint = self.heightAnchor.constraint(equalToConstant: 75)
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
heightConstraint.isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
PlaygroundPage.current.liveView = ViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment