how can I refresh data in a collection view in swift 4?
11,064
UICollectionView
has multiple ways of reloading data.
If you want every cell to be reloaded:
collectionView.reloadData()
.
If you want a specific section to be reloaded: collectionView.reloadSections(<Array of Sections>)
.
If you want to reload specific cells: collectionView.reloadItems(<Array of IndexPaths>)
.
Author by
Erick Arciniega
Updated on August 30, 2022Comments
-
Erick Arciniega over 1 year
I have a function to refresh the data of an array every 2 seconds
var timer = Timer() func timeRefresh(){ timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(ViewController.refreshData), userInfo: nil, repeats: true) }
I receive the data from a post service
@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 = ["-.-","-.-","-.-","-.-","-.-"] } } }
I need refresh the collection view every second
cell.typeLabel.text = timeCar[indexPath.row]
-
V D Purohit over 5 yearsreload in func timeRefresh() as per jake answer.
-
-
Erick Arciniega over 5 yearsin my collectionview i have a image and two labels and i want to refresh only one label, that is posible?
-
Ravi over 5 years@ErickArciniega when you update your data object and reload the collection view , collection view will automatically update the view . collectionView.reloadData() should help after updating data object