UILabel将不会使用计时器更新

时间:2019-07-31 21:12:18

标签: swift uitableview timer uilabel nsattributedstring

每当时间改变一分钟,我便试图使用此代码来更新自定义单元的uilabel的时间。

shark.data<-read.csv("sharks_nosquatinis_format.csv",row.names=1)
dotTree(str_tree_sharks,shark.data,colors=setNames(c("blue","red"),
c("yes","no")),ftype="i",fsize=0.7)

这是dateIn函数,用于以NSAttributedString的形式返回当前时间:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { [weak self] _ in
        if let s = self, let tableView = s.tableView, let indexPaths = tableView.indexPathsForVisibleRows {
            for indexPath in indexPaths {
                guard let cell = tableView.dequeueReusableCell(withIdentifier: "PersonCell", for: indexPath) as? PersonCell else { return }
                let person = s.people[indexPath.section]
                cell.time.attributedText = dateIn(timeZone: person.timeZone)
            }
        }
    })
}

Time是一个UILabel,它是PersonCell类的一部分。由于某些原因,尽管dateIn(timeZone:person.timeZone)的值确实发生了更改(我使用print语句进行了检查),但时间更改时UILabel的内容不会更新。我在互联网上四处张望,但找不到一个使用reloadData()的解决方案,但我不确定这是否对内存和速度都有好处。

1 个答案:

答案 0 :(得分:0)

您必须调用另一个函数来更改所需单元格中的值。

使用此

let cell = tableView.cellForRow(at: indexPath)

在您当前的实现中,单元只是一个本地创建的变量。这不是您想要的单元格。

相关问题