我有UITableViewcells那些是在不同的时间创建的,我希望他们每个人都有一个独立的计时器,在添加对象时触发reloadData()
这是我到目前为止所做的
import UIKit
var timer = Timer()
var blinkStatus:Bool! = false
var time = 300
class LiveViewCell: UITableViewCell {
let str = String(format:"%02d:%02d", (time / 60), (time % 100))
func processTimer() {
if time > 0 {
time -= 1
timeRemainingLbl.text = String(time)
} else if time == 0 {
timer.invalidate()
timeRemainingLbl.text = "Due!"
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(LiveViewCell.blinkTimer), userInfo: nil, repeats: true)
}
}
func blinkTimer() {
if blinkStatus == false { …Run Code Online (Sandbox Code Playgroud) 我有以下功能.我正在尝试将allItems数组传递给完成块requestItems,但是我发现它崩溃,因为它说它是零.我删除了完成以检查它item是否有值,它确实存在.
这意味着完成在for循环之前执行.
还有另一种方法吗?类似于Javascript中的Promises,它将在for循环结束时执行完成.
func requestItems(_ data: [String: Any], completion: (Bool, [Item]) -> Void) {
var allItems = [Item]()
for i in data["all"] {
Routes.instance.getRequest(requestType: "items", params: nil, id: someId, completion: { item in
var it = Item(item["name"] as! String)
allItems.append(it)
})
}
completion(true, allItems)
}
func getRoutes(requestType: String, parameters: [String: Any]?, id: String, completion: @escaping ([[String:Any]]) -> Void) {
DispatchQueue.main.async {
if id == "" {
self.url = "\(URL_BASE)/\(requestType)"
} else {
self.url = "\(URL_BASE)/\(requestType)/\(id)"
} …Run Code Online (Sandbox Code Playgroud)