如何在 swift 4 的集合视图中刷新数据?
how can I refresh data in a collection view in swift 4?
我有一个函数,每 2 秒刷新一次数组的数据
var timer = Timer()
func timeRefresh(){
timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(ViewController.refreshData), userInfo: nil, repeats: true)
}
我从 post 服务接收数据
@objc func refreshData(){
post(postString, Route) { (res) in
let success = res["success"]
if success == true {
let walkers = res["walkers"]
for secondItem in walkers.array! {
let duration = secondItem["duration"]
self.timeCar.append(duration.stringValue) //this is the info that i need for the collection view
}
}
print("array time \(self.timeCar)")
}else{
self.timeCar = ["-.-","-.-","-.-","-.-","-.-"]
}
}
}
我需要每秒刷新一次集合视图
cell.typeLabel.text = timeCar[indexPath.row]
UICollectionView
有多种重新加载数据的方式。
如果您希望重新加载每个单元格:
collectionView.reloadData()
.
如果您想要重新加载特定部分:collectionView.reloadSections(<Array of Sections>)
。
如果要重新加载特定单元格:collectionView.reloadItems(<Array of IndexPaths>)
。
我有一个函数,每 2 秒刷新一次数组的数据
var timer = Timer()
func timeRefresh(){
timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(ViewController.refreshData), userInfo: nil, repeats: true)
}
我从 post 服务接收数据
@objc func refreshData(){
post(postString, Route) { (res) in
let success = res["success"]
if success == true {
let walkers = res["walkers"]
for secondItem in walkers.array! {
let duration = secondItem["duration"]
self.timeCar.append(duration.stringValue) //this is the info that i need for the collection view
}
}
print("array time \(self.timeCar)")
}else{
self.timeCar = ["-.-","-.-","-.-","-.-","-.-"]
}
}
}
我需要每秒刷新一次集合视图
cell.typeLabel.text = timeCar[indexPath.row]
UICollectionView
有多种重新加载数据的方式。
如果您希望重新加载每个单元格:
collectionView.reloadData()
.
如果您想要重新加载特定部分:collectionView.reloadSections(<Array of Sections>)
。
如果要重新加载特定单元格:collectionView.reloadItems(<Array of IndexPaths>)
。