在 cellForRowAtIndexPath 中将 Int 数组格式化为字符串 Swift

Format Int Array as String in cellForRowAtIndexPath Swift

我正在获取以秒为单位存储在 Int 数组中的时间值,并尝试将其放在 UILabel 上,然后由 countDown 函数触发倒计时。我无法获得在每个单元格中倒计时的时间,也无法打印数组而不只获取原始秒值而不对其进行格式化,如下所示。现在我只能在每个单元格的 UI 标签上显示原始值:6532(小时、分钟、秒)

尝试在 for loop 中单独打印数组时,我无法格式化数组,如下所示。该数组存储为 Int.

在 countDown 函数中使用 for 循环时,我可以将格式化的时间打印到日志中:HH:MM:SS。但并非如此。

这是我的倒计时函数:

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])
    }
}

我正在尝试在 cellForRowAtIndexPath 方法中执行此操作。 UI标签位于单独的 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
}

如何格式化 intArray 以便它打印到日志和 UI 作为格式化时间 (HH:MM:SS) 就像在 for loop 中一样我使用 字符串格式化程序 ?

打印到日志

非常感谢任何帮助!

您需要创建一个函数,将秒数转换为格式化字符串,例如

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)
}

获得此方法后,您可以在 countDown 方法中使用此方法以及 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])
    }
}

cellForRowAtIndexPath 中,您有 timerInt 作为 Int 数组,其中每个索引值都需要显示在单元格 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
    }

在@vacawama 的大量帮助下,这最终为我工作:

  1. 这就是我查询日期的方式,格式化日期将如何存储在数组中,然后将其放入名为 intArray 的数组中。

    var 创建时间 = object.createdAt 如果 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. 其次,我确保将 timer 变量保留在查询之外,这样计时器就不会根据我查询的对象数量进行倒计时。

  3. 我的倒计时函数完成了所有格式设置:

    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. cellForRowAtIndexPath 函数中,我根据数组 intArray 格式化标签在 UI 上的显示方式,如下所示:

       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)
    
        }
    

一切正常并按需要显示:HH:MM:SS。