Tiptech
Tiptech

Reputation: 177

Handling multiple timer values in a UITableViewController?

I don't necessarily need code, but I would appreciate pretty specific steps/logic on how to handle my problem.

My initial view controller is a table view controller. It will show the individual timer value and display its counting down in the current cell. The timer value is retrieved from a separate regular view controller. This view controller contains a UIPicker and the timer value is calculated using the user-selected values from the picker.

I am having trouble with the logic for handling multiple timers in the tableviewcontroller. Each newly created cell should be assigned a new timer object that is created with the creation of that cell. This way, a specific cell uses the time value created with a specific UIPicker value. So far, I can only get it to where each cell uses the same timer value as the first cell that is created.

How do I tell a newly created cell located in my initial table view controller that it should only use the value created with its specific timer value selected by the user with the UIPicker located in the second view controller? How do I separate the timer values between cells?

Upvotes: 2

Views: 783

Answers (3)

Hitesh Surani
Hitesh Surani

Reputation: 13537

Please use below instead of NSTimer.

Solution : 1

int64_t delayInSeconds = gameInterval; // Your Game Interval as mentioned above by you

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

    // Update your label here. 

});

Solution : 2

NSTimer.scheduledTimerWithTimeInterval(1, 
                    target: NSBlockOperation(block: {...}), 
                    selector: #selector(NSOperation.main), 
                    userInfo: nil, 
                    repeats: true)

Upvotes: 0

Jeff Woo
Jeff Woo

Reputation: 86

You should never have a table view cell with logical implementations, so at the most, the interface of the cell should have a method called setTime, and you give it a String which it just assigns it to the label.

Now that this is cleared up, who should be doing the logic? I believe you can abstract the timer logic into their own objects, because it would be too much of a hassle for the table view controller to keep track of all the play/pause states, how much time has passed etc.

So the work that's left for table view controller is just to get the data (remaining time) from the timer objects, transform them into Strings (you might have formatting logic here that VCs should handle) then pass those strings to the cell.

The code would look like this:

var timerObject = timerObjectArray[rowNumber]
var timeRemaining: Date = timerObject.getTimeRemaining()
var timeString: String = convertToString(timeRemaining)
cell.setTimeString(timeString)

So now we try to figure out when/how to get the cells. As for when, that would be your choice, I think a NSTimer.scheduledTimerWithInterval could work (be careful of retain cycle)

Now in the method you pass to the schedule to be run every cycle, you should have code like this:

self.tableView.beginUpdates()
var visibleCells = self.tableView.visibleCells
for cell in visibleCells {
    var indexPath = self.tableView.indexPath(for: cell)
    var rowNumber = indexPath.row

    var timeRemainString = getTimeRemainingStringFor(row: rowNumber)
    cell.setTimeString(timeRemainString)
}
self.tableView.endUpdates()

Hope this helped! I probably missed something so just ask

Upvotes: 2

Amit Tandel
Amit Tandel

Reputation: 883

I think your problem is maintaining state for each row data. Since table view reuses the cell, it becomes a problem. Save the state of the timers in an array in parent view controller. Set that value in every call of cellForRowAtIndex.

Upvotes: 1

Related Questions