Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIFT - reload UITableViewCell

I have run into a problem when trying to reload my UITableView from within my TimersManager.swift file. The TimersManager.swift is used to control/manage all the timers in my to-do list/timers app. I am trying to update the UILabel to show the updated time as the timer ticks away. For some reason it will not update the table. Please have a look below and hopefully you can give me a nudge in the right direction. Thanks.

top of listTableViewController.swift:

var prepMgr: listTableViewController = listTableViewController()
var cell:customCell!

 class listTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {

update func in listTableViewController: (this is called by another func in my TimersManager.swift file)

    func update (indexPathRow: Int) {

    for task in taskMgr.tasks {

        if task.timerOn == true {

        //calculate the time to display in 0:00:00 format.
        let date1 : NSDate = task.timerFinishDate
        let date2 : NSDate = NSDate()
        let compareResult = date1.compare(date2)
        let length = Int(round(date1.timeIntervalSinceDate(date2)))

        var tmpHours = length / 3600
        var tmpMinutes = (length % 3600) / 60
        var tmpSeconds = length % 60

        var timeString = "\(tmpHours):\(tmpMinutes):\(tmpSeconds)"

        println(task.subText) //test, display old value before update - WORKS

        taskMgr.updateTask(indexPathRow, name: taskMgr.tasks[indexPathRow].name, subText: timeString, timerOn: taskMgr.tasks[indexPathRow].timerOn, completed: taskMgr.tasks[indexPathRow].completed, timerFinishDate: taskMgr.tasks[indexPathRow].timerFinishDate, taskID: taskMgr.tasks[indexPathRow].taskID, sliderHours: taskMgr.tasks[indexPathRow].sliderHours, sliderMinutes:taskMgr.tasks[indexPathRow].sliderMinutes, sliderSeconds: taskMgr.tasks[indexPathRow].sliderSeconds)

            println(task.subText) //test, display updated value after update - WORKS
            println(timeString) //test, display time remaining in timer 0:00:00 - WORKS
        }
        self.tableView.reloadData() // DOES NOT UPDATE TABLE.

    }
}

the code for the NSTimer selector in TimersManager.swift:

    func tickTock (length:NSTimer!) {
    println(length.userInfo)

    var count = 0
    for timer in timers {

                let date1 : NSDate = timer.fireDate
                let date2 : NSDate = NSDate()
                let compareResult = date1.compare(date2)
                let length = Int(round(date1.timeIntervalSinceDate(date2)))

                if length <= 0 {
                        //Invalidate NSTimer
                        timer.myTimer.invalidate()
                        //Remove from array
                        timers.removeAtIndex(count)
                        }
        count++
        println(length) //test, shows how many seconds are left - WORKS

        //update labels.
       prepMgr.update(timer.indexPathRow) //Call to listTableViewController func - Half working, calls the function. updates the correct task. But table is not reloaded.

    }


    //update labels, reload table
    prepMgr.tableView.reloadData() //Test, Not working
}
like image 254
the_pantless_coder Avatar asked Mar 28 '26 18:03

the_pantless_coder


1 Answers

You could also use a NSNotification to handle the "Reload Function" of the Table. And just call them if you need an update of your table.

like image 72
derdida Avatar answered Apr 02 '26 11:04

derdida



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!