0

I am taking time values stored in seconds in an Int array and attempting to put it on a UILabel which are then counting down trigged by a countDown function. I'm unable to get the time to count down in each cell, nor am I able to print the array without getting only the raw seconds value without formatting it as you'll see below. For now I can only display the raw value on a UILabel per cell as: 6532 (the hours, minutes, seconds)

I can't format the array either when trying to print it alone in the for loopas you'll see below. The array is stored as an Int.

When using a for loop in the countDown function, I can print to the logs the time formatted: HH:MM:SS. But not otherwise.

Here's my countDown function:

func countDown() {
    //timerInt is an array where I'm storing the Int values.
    for i in 0 ..< timerInt.count {
        let hours = timerInt[i] / 3600
        let minsec = timerInt[i] % 3600
        let minutes = minsec / 60
        let seconds = minsec % 60
        print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))

        timerInt[i]--
        print(self.timerInt[i])
    }
}

I'm trying to do this in a cellForRowAtIndexPath method. The UILabel is located in a separate class:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell


 //This is the first item i'm posting on a UILabel and it works fine.
    myCell.productName.text = productName[indexPath.row]

        /*This cell/label I'm unable to format like in the for loop 
          which is formatted as a string*/

         myCell.secondLabel.text = ("\(intArray[indePath.row])")

         /*how do I format the intArray here so that the time 
           appears HH:MM:SS?? */



    return myCell
}

How can I format the intArray so that it prints to the logs and to the UI as a formatted time (HH:MM:SS) like in the for loop when i print to the logs with the String formatter?

Any help is much appreciated!

DuncanKinnear
  • 4,241
  • 2
  • 31
  • 62
Lukesivi
  • 2,136
  • 4
  • 23
  • 42

2 Answers2

0

You need to create a function which will translated seconds into formatted string like

func formattedTime(seconds: Int = 0) -> String {
        let hours = seconds/3600
        let minsec = seconds%3600
        let minutes = minsec / 60
        let seconds = minsec % 60
        return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}

Once you have this method you can use this method inside the your countDown method along with cellForRowAtIndexPath

func countDown() {
    //timerInt is an array where I'm storing the Int values.
    for i in 0 ..< timerInt.count {
        print(formattedTime(timerInt[i]))
        timerInt[i]--
        print(self.timerInt[i])
    }
}

Inside the cellForRowAtIndexPath you have timerInt as Int array where each index value need to be shown on cell UILabel

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell


     //This is the first item i'm posting on a UILabel and it works fine.
        myCell.productName.text = productName[indexPath.row]

            /*This cell/label I'm unable to format like in the for loop 
              which is formatted as a string*/

             myCell.secondLabel.text = ("\(intArray[indePath.row])")

             /*how do I format the intArray here so that the time 
             */appears HH:MM:SS??
        print(self.formattedTime(self.intArray[indePath.row]))
        return myCell
    }
deoKasuhal
  • 2,627
  • 17
  • 26
  • Thanks for the answer. I'm getting an error on these two lines in the `cellForRowAtIndexPath` `myCell.secondLabel.text = ("\(intArray[indexPath.row])")` `print(self.formattedTime(self.intArray[indexPath.row]))` The error is that there was nil when unwrapping an optional value. Seems strange, unable to get rid of it... – Lukesivi Oct 29 '15 at 10:19
  • @rinyfo4, please refer [this](http://stackoverflow.com/questions/24643522/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-values) for error – deoKasuhal Oct 29 '15 at 15:54
0

This is what ended up working for me with a lot of help from @vacawama:

  1. This is how I'm querying the date, formatting how the date will be stored in the array and then placing it into an array called intArray.

    var createdAt = object.createdAt if createdAt != nil {

                let calendar = NSCalendar.currentCalendar()
                let comps = calendar.components([.Hour, .Minute, .Second], fromDate: createdAt as NSDate!)
                let hour = comps.hour  * 3600
                let minute = comps.minute * 60
                let seconds = comps.second
    
    
                self.timerInt.append(hour + minute + seconds)
    
    
            }
    
    
            }
    
        }
    
    
         var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDown"), userInfo: nil, repeats: true)
    
    })
    self.tableView.reloadData()
    

    }

  2. Second, I made sure to keep the timer variable outside of the query so that the timer doesn't count down based on how many objects I'm querying.

  3. My countDown function did all the formatting:

    func countDown() {
    
        //timerInt is an array where I'm storing the Int values.
        for i in 0 ..< timerInt.count {
    
            let hours = timerInt[i] / 3600
            let minsec = timerInt[i] % 3600
            let minutes = minsec / 60
            let seconds = minsec % 60
       print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))
          timerInt[i]--
            self.tableView.reloadData()
    
        }
    

    }

  4. In the cellForRowAtIndexPath function, I formatted how the label should be displayed on the UI based on the array intArray as below:

       override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell
    
       //Formatting how the cell will be presented
        let hours = timerInt[indexPath.row] / 3600
        let minsec = timerInt[indexPath.row] % 3600
        let minutes = minsec / 60
        let seconds = minsec % 60
    
    
      myCell.secondLabel.text = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
    
        }
    

Everything works perfectly and is displayed as wanted: HH:MM:SS.

Lukesivi
  • 2,136
  • 4
  • 23
  • 42