![](/img/trans.png)
[英]How to change particular UITableviewcell colour in iOS swift4
[英]How to change a label text during a for loop in IOS swift4?
我要在運行for循環時更改/重新加載活動指示器上的標簽。 但是我無法更改標簽文本。
for i in 0..<(tempAry.count)
{
self.tempLbl.text = "\(i) Task Completed"
}
您不能每次都從for循環中看到標簽文本,您只會看到最后一個索引標簽。
此for循環的時間復雜度為O(1),因此整個for循環的執行時間以毫秒為單位。
因此,更好的方法是使用計時器在標簽上顯示文本。
全局定義。
var count = 0
var timer: Timer?
在viewDidLoad
方法中添加計時器。
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updating), userInfo: nil, repeats: true)
外部ViewDidLoad
方法放在下面的方法中。
@objc func updating() {
if count >= 100 - 1{
timer?.invalidate()
timer = nil
return
}
count += 1
self.title = "\(count) Task Completed"
}
實際上以前添加的答案工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.