class ViewController: UIViewController {
var tableView = UITableView()
var timersData: [TimersData] = []
let sections: [String] = ["Добавление таймеров", "Таймеры"]
var addTimers = AddTimersCell()
var timers = TimersCell()
// var timer = Timer()
struct Cells {
static let addTimersCell = "AddTimersCell"
static let timersCell = "TimersCell"
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Мульти таймер"
timersData = fetchTimers()
configureTableView()
}
func configureTableView() {
view.addSubview(tableView)
setTableViewDelegates()
tableView.register(AddTimersCell.self, forCellReuseIdentifier: Cells.addTimersCell)
tableView.register(TimersCell.self, forCellReuseIdentifier: Cells.timersCell)
tableView.pin(to: view)
}
func setTableViewDelegates() {
tableView.delegate = self
tableView.dataSource = self
}
func timersCellAdd(_ controller: ViewController, didAdd item: TimersData) {
let newRowIndex = timersData.count
timersData.append(item)
let indexPath = IndexPath(row: newRowIndex, section: 1)
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.reloadData()
}
@objc func add() {
_ = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in
DispatchQueue.main.async {
let countDown = Int(self?.addTimers.timeInSecTextField.text! ?? "0")
if var countDown = countDown {
countDown -= 1
}
self?.timers.timerLabel.text = String(countDown ?? 0)
}
}
// let item = TimersData(nameTimer: addTimers.nameTextField.text!, timer: addTimers.timeInSecTextField.text!)
// timersCellAdd(self, didAdd: item)
//
tableView.reloadData()
print("tap")
}
@objc func start() {
var countDown = 5.0
countDown -= 1.0
timers.timerLabel.text = addTimers.timeInSecTextField.text
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1:
return timersData.count
default:
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: Cells.addTimersCell, for: indexPath) as! AddTimersCell
cell.addButton.addTarget(self, action: #selector(add), for: .touchUpInside)
cell.nameTextField.text = timers.nameLabel.text
cell.timeInSecTextField.text = timers.timerLabel.text
tableView.rowHeight = 230
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: Cells.timersCell, for: indexPath) as! TimersCell
let timersData = timersData[indexPath.row]
cell.set(timersData: timersData)
cell.nameLabel.text = addTimers.nameTextField.text
cell.timerLabel.text = addTimers.timeInSecTextField.text
tableView.rowHeight = 50
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
tableView.deselectRow(at: indexPath, animated: true)
cell?.selectionStyle = .none
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return sections[0]
default:
return sections[1]
}
}
}
extension ViewController {
func fetchTimers() -> [TimersData] {
let item = TimersData(nameTimer: addTimers.nameTextField.text!, timer: addTimers.nameTextField.text!)
return [item]
}
}