将Int Array格式化为cellForRowAtIndexPath Swift中的String

时间:2015-10-28 20:36:10

标签: arrays string swift nsdate nsdateformatter

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

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

在countDown函数中使用for循环时,我可以在日志中打印格式化的时间:HH:MM:SS。但不是这样。

这是我的countDown函数:

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方法中执行此操作。 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?? */



    return myCell
}

我如何格式化intArray以便将其打印到日志和UI作为格式化时间(HH:MM:SS),就像在for loop中打印到日志时一样 String formatter

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

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

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

cellForRowAtIndexPathtimerIntInt数组,其中每个索引值都需要在单元格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
    }

答案 1 :(得分:0)

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

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

    var createdAt = 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. 我的countDown函数执行了所有格式化:

    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)
    
        }
    
  5. 一切都很完美,并按照需要显示:HH:MM:SS。