How to automatically refresh table view in view controller upon entering screen? - swift

I'm able to make use of refreshControl and pull down to manually update the screen, but how can i update it automatically every time i enter the screen without having to pull down.
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
configureRefreshControl()
tableView.refreshControl = refreshControl
}
func configureRefreshControl() {
self.refreshControl.addTarget(self, action: #selector(handleRefreshControl), for: .valueChanged)
self.refreshControl.tintColor = UIColor.white
}
#objc func handleRefreshControl() {
updateCommentsOverview()
}

You can call tableView.reloadData() or updateCommentsOverview() either on viewDidLoad. Which will update once Per screen launch. Or call it on viewWillAppear. Which will cause an update every-time you “see” the screen, meaning even when you come back from the background.
I can only guess you mean to use viewWillAppear.
See - https://developer.apple.com/documentation/uikit/uiviewcontroller/1621510-viewwillappear for more info

Related

Refresh animation not displaying when reloading tableview | Swift

I cannot seem to figure out why the loading animation stop appearing after I leave and come back to the UITableViewController the following is what I am seeing once I come back - the pull-down gesture still executes the reload code and refreshes the UITableView, just the animation is not showing.
What I am seeing:
What I am doing:
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl?.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.backgroundColor = .tertiarySystemGroupedBackground
edgesForExtendedLayout = [.top]
extendedLayoutIncludesOpaqueBars = true
}
#objc func refresh(sender:AnyObject)
{
self.fetchJSON()
self.refreshControl?.endRefreshing()
}
Setup in Storyboards:
Is there something I am doing incorrect that would prevent the loader from appearing? To clarify it is showing on initial load, but then fails to appear when return
Try closing your refresh control from the main thread.
DispatchQueue.main.async { self.refreshControl.endRefreshing() }

UIRefresh Control UI bug with large titles & collection view

I am using a UICollectionViewController embedded within a UITabBarController & UINavigationController and when I switch between tabs multiple times I am occasionally experiencing a bug where the refresh control appears above the title when I do not want it to.
Refresh Control UI Bug
Debug View Hierachy
var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.tintColor = UIColor.primaryRed
return refreshControl
}()
refreshControl.addTarget(self, action: #selector(refreshData), for: .valueChanged)
collectionView?.refreshControl = refreshControl
#objc private func refreshData() {
refreshControl.beginRefreshing()
datasource.fetchEvents {
self.refreshControl.endRefreshing()
self.collectionView?.reloadData()
}
}

swift reload data UICollectionView

var refreshControl: UIRefreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
collectionview.delegate = self
collectionview.dataSource = self
refreshControl.addTarget(self, action: #selector(HomeViewController.refreshData), for: UIControlEvents.valueChanged)
fetchPosts()
collectionview.addSubview(refreshControl)
}
#objc func refreshData() {
refreshControl.endRefreshing()
}
I have a UITabBar with some ViewController. In a ViewController i have a UICollectionView where there are images of users. Every users can load an image and this image is seen in the controller with UICollectionView. once the photo is loaded, in the UICollectionView it does not appear because the data must be refreshed. In the function RefreshData what should I do to refresh the UICollectionView?
You can refresh the collection view by using:
#objc func refreshData() {
collectionview.reloadData()
refreshControl.endRefreshing()
}
You can also consider to use a simple image caching library, as AlamofireImage for example, to avoid full collection view refresh for this.
You simply need to call reloadData() for collectionView, i.e.
#objc func refreshData()
{
self.collectionView.reloadData()
refreshControl.endRefreshing()
}
You must have an IBOutlet for collectionView in your ViewController.
#objc func refreshData()
{
self.arr.removeAll()
fetchPosts()
self.collectionView.reloadData()
refreshControl.endRefreshing()
}
API Call
func fetchPosts() {
self.arr.removeAll()
//API call code..
//after success response you need to reload collectionView
}
you need to call api when you want to refresh so that you will get updated data.and more thing in API response you also need to self.collectionView.reloadData() when you get successfull response.

Why won't my tableview cell load when I run this app?

In this simple project! I am using a tableview and trying to display one cell but when I run the app it doesn't show the one cell I want it to. I don't know what I did wrong. I think it has something to do with the constraints. I'm lost!
You need to set the delegate / data source and then reload the tableview within view did load
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
tableview.reloadData()
}

How to implement edit/delete behaviour on an embedded tableView?

So I have an embedded tableview and I want to implement edit/delete behaviour:
In my HomePageViewController I have:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = editButtonItem
}
However, all that happens is when I click on the edit button, it says done, and the embedded tableview does nothing at all. When I put the above code in the tableview nothing happens.
How do I get the navigation controller/parent view controller to recognise the embedded table view?
It looks like you're using an embed segue to embed a UITableViewController. In your parent view controller, you can do 1 of 2 things easily to achieve you goal.
Method 1: Use the child view controller's edit button
override func viewDidLoad() {
super.viewDidLoad()
// Find and (optionally assign it to a variable for later convenience) the embedded controller, IBOutlets aren't available for VCs embedded within a storyboard
let childControllers = childViewControllers.filter { return $0 is EventTableViewController }
let embeddedController = childControllers[0] as! EventTableViewController
navigationItem.leftBarButtonItem = embeddedController.editButtonItem
}
Method 2: Forward edit events to child view controllers
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = editButtonItem
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
// Forward editing state to children
childViewControllers.forEach { $0.setEditing(editing, animated: animated) }
}
Note: editButtonItem was exposed in iOS 10 (but was implemented much earlier). For deployment targets less than iOS 10, you can use a custom edit button combined with method 2.
In this situation, you can't use the automatic behavior of the edit button. It's up to you to implement it yourself. You need to toggle both the isEditing of the table view and the appearance of the button. Here's an example from one of my own apps:
func doEdit(_ sender: Any?) {
var which : UIBarButtonSystemItem
if !self.tableView.isEditing {
self.tableView.setEditing(true, animated:true)
which = .done
} else {
self.tableView.setEditing(false, animated:true)
which = .edit
}
let b = UIBarButtonItem(barButtonSystemItem: which,
target: self, action: #selector(doEdit))
self.navigationItem.rightBarButtonItem = b
}