Я новичок в Xcode и Swift, и сейчас я пытаюсь создать приложение для iOS, которое позволит пользователю создавать несколько пользовательских countdown timers
. Я использую UITableViewController
с UITableViewCell
и UIViewController
для добавления новых timers
. Но я не могу понять, как реализовать их, работая вместе. И как реализовать в пользовательских настройках таймера ячейки.
Моя ячейка UITableViewController:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "timer", for: indexPath) as! TimerTableViewCell
cell.timerLabel.text = timersNames[indexPath.row]
let sec = timersSeconds[indexPath.row]
cell.timerTime.text = formattedTime(time: TimeInterval(sec))
print(sec)
return cell
}
Моя пользовательская ячейка UITableViewCell с функциями таймера:
class TimerTableViewCell: UITableViewCell {
var delegate: TimerCellDelegate?
@IBOutlet var timerLabel: UILabel!
@IBOutlet var timerTime: UILabel!
@IBOutlet var playButton: UIButton!
@IBOutlet var pauseButton: UIButton!
@IBOutlet var resetButton: UIButton!
var seconds = 10
var timer = Timer()
var hasTimerStarted = false
var isResumed = false
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self,
selector: (#selector(TimerTableViewCell.updateTimer)), userInfo: nil, repeats: true)
}
@objc func updateTimer() {
if seconds < 1 {
timer.invalidate()
//Send alert to indicate "time's up!"
} else {
seconds -= 1
timerLabel.text = formattedTime(time: TimeInterval(seconds))
}
}
func formattedTime(time:TimeInterval) -> String {
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
}
@IBAction func timerStart(_ sender: UIButton) {
delegate?.didTapTimerStart(sender.tag)
if hasTimerStarted == false {
runTimer()
self.playButton.isEnabled = false
}
}
@IBAction func timerPause(_ sender: UIButton) {
delegate?.didTapTimerPause(sender.tag)
if self.isResumed == false {
timer.invalidate()
hasTimerStarted = false
self.isResumed = true
self.pauseButton.setTitle("Resume",for: .normal)
} else {
runTimer()
self.isResumed = false
hasTimerStarted = true
self.pauseButton.setTitle("Pause",for: .normal)
}
}
@IBAction func timerReset(_ sender: UIButton) {
delegate?.didTapTimerReset(sender.tag)
timer.invalidate()
seconds = 10
timerLabel.text = formattedTime(time: TimeInterval(seconds))
hasTimerStarted = false
isResumed = false
self.pauseButton.setTitle("Pause",for: .normal)
pauseButton.isEnabled = false
self.playButton.isEnabled = true
}
}
Как поместить код из настраиваемой ячейки в конфигурацию ячейки UITableViewController и заставить их работать вместе?
Спасибо.