【问题标题】:Update custom tableviewcell label through NSTimer every 1 second in Swift 3在 Swift 3 中每 1 秒通过 NSTimer 更新自定义 tableviewcell 标签
【发布时间】:2016-11-16 11:45:22
【问题描述】:

我有一个自定义的TableViewCell。 我希望每个单元格中的计时器标签每秒减少一次。我提到了this 链接。唯一的问题是我无法更改标签的文本。如果我在控制台中打印它,值就会很好。从自定义单元格类更改单元格的值后,如何重新加载TableView? 我是自定义TableView 单元格的新手,所以请帮忙。 我也提到了this链接。

class CustomCell: UITableViewCell {

@IBOutlet weak var timerLabel: UILabel!

var timer = Timer()

func updateRow(){
    print("started timer")
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(CustomCell.decreaseTimer), userInfo: nil, repeats: true)
}

func decreaseTimer(){
    let cell = self
    let timerVal = cell.timerLabel.text
    print("decrease "+timerVal!)
    //how to reflect this timerVal value in the cell?
}}

【问题讨论】:

  • 您能否发布一些与 tableView 单元格和计时器相关的代码,以便我们了解并在此基础上为您提供帮助。
  • 我已经添加了CustomCell代码

标签: ios swift uitableview nstimer tableviewcell


【解决方案1】:

您需要为标签分配更新时间值。

func decreaseTimer(){
    let cell = self
    let timerVal = cell.timerLabel.text
    print("decrease "+timerVal!)
    //how to reflect this timerVal value in the cell?
    cell.timerLabel.text = timerVal 
}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多