Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class TimersViewController: UIViewController {
- @IBOutlet weak var tableView: BaseTableView!
- var timerArr = [Int]()
- let cellId = "cellId"
- override func viewDidLoad() {
- super.viewDidLoad()
- view.backgroundColor = .blue
- setupTableView()
- setupNavigationBar()
- }
- fileprivate func setupNavigationBar() {
- let add = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(onAddClicked))
- navigationItem.rightBarButtonItems = [add]
- }
- @objc
- fileprivate func onAddClicked() {
- self.timerArr.append(100)
- tableView.reloadAsync()
- }
- fileprivate func setupTableView() {
- tableView.backgroundColor = UIColor.red.withAlphaComponent(0.3)
- tableView.delegate = self
- tableView.dataSource = self
- tableView.register(CustomCell.self, forCellReuseIdentifier: cellId)
- }
- }
- extension TimersViewController: UITableViewDelegate,UITableViewDataSource {
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return self.timerArr.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomCell
- cell.configure(with: self.timerArr[indexPath.row])
- cell.handler = {[weak self] (counter) in
- print(indexPath.row , counter)
- self?.timerArr[indexPath.row] = counter
- }
- return cell
- }
- }
- class CustomCell: UITableViewCell {
- //MARK: Outlets
- @IBOutlet weak var label: UILabel!
- //MARK: Internal Properties
- var handler: ((Int)->())?
- //MARK: Private Properties
- private var timer: Timer?
- private var counter = 100 {
- didSet {
- DispatchQueue.main.async {
- self.lblTitle.text = "\(self.counter)"
- self.handler?(self.counter)
- }
- }
- }
- fileprivate var lblTitle: UILabel = {
- let lbl = UILabel()
- lbl.font = UIFont.systemFont(ofSize: 50)
- return lbl
- }()
- override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
- super.init(style: style, reuseIdentifier: reuseIdentifier)
- commonInit()
- }
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- commonInit()
- }
- fileprivate func commonInit() {
- self.setupView()
- }
- //MARK: Internal Methods
- func configure(with counter: Int) {
- self.lblTitle.text = "\(counter)"
- self.counter = counter
- self.setTimer()
- }
- //MARK: Private Methods
- private func setTimer() {
- self.timer?.invalidate()
- self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: {[weak self] (timer) in
- if let counter = self?.counter, counter > 0 {
- self?.counter -= 1
- } else {
- timer.invalidate()
- }
- })
- }
- fileprivate func setupView() {
- //This sets the label equal to superview with padding
- lblTitle.safeArea(equalTo: self, padding: 10)
- }
- }
- class BaseTableView: UITableView {
- override init(frame: CGRect, style: UITableView.Style) {
- super.init(frame: frame, style: style)
- commonInit()
- }
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- commonInit()
- }
- fileprivate func commonInit() {
- tableFooterView = UIView()
- }
- }
- extension UITableView {
- func reloadAsync() {
- DispatchQueue.main.async {
- self.reloadData()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement