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.
Related
I have a UIView which displays some information such as a user's Name and more, including a list of objects that all get pulled from my database. This works fine.
However, I now have a ViewController that gets presented on top of the current ViewController. In this presented ViewController, I am adding Data to my Database. When dismissing that view, I want the original ViewController to update all of its content to be up to date.
Right now, all my views are getting layedout in ViewDidLoad, meaning that they only really get loaded once and don't reload later on. I have managed to update Layout by calling self.view.layoutIfNeeded(), but if I understand correctly, this only updates constraint. Of course, I could call a new init of my original view controller. This would make it reload, but I would like to avoid that.
Another Idea I had was to set up all my content in the ViewWillAppear, which should maybe then update anytime my view controller is about to be visible. However, I don't know how to go about doing this. Can I just move all my setup code to viewWillAppear? Does this have any disadvantages?
TLDR: Is there a way to update a stackview with new elements without having to reload the full ViewController over ViewWillAppear?
The UITableView element works very smoothly with database data. If you fetch the data from your database inside viewDidLoad in your first view controller, and store it in an array, the UITableView (if you set up its dataSource correctly) will automatically populate the table with the new values from the second view controller. With this method, there is no need to use ViewWillAppear at all.
It sounds like as of now, you're using Views (inside a VStack)? to display individual objects from the database. If you want to keep whatever custom style/layout you're using with your views, this can be done by defining a custom subclass of UITableViewCell and selecting the "Also create XIB file" option. The XIB file lets you customize how the cells in your UITableView look.
Here is a simple example to show the database values in the first view controller automatically updating. I didn't include the custom XIB file (these are all default UITableViewCells), to keep it streamlined.
FIRST VIEW CONTROLLER
import UIKit
import CoreData
class ViewController: UIViewController {
#IBOutlet weak var dataTable: UITableView!
var tableRows: [DataItem] = []
func loadData() {
let request: NSFetchRequest<DataItem> = DataItem.fetchRequest()
do {
tableRows = try Global_Context.fetch(request)
} catch {
print("Error loading data: \(error)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
dataTable.dataSource = self
loadData()
}
#IBAction func goForward(_ sender: UIButton) {
self.performSegue(withIdentifier: "toSecond", sender: self)
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableRows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dataTableCell", for: indexPath)
cell.textLabel?.text = tableRows[indexPath.row].name
return cell
}
}
let Global_Context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func saveContext () {
if Global_Context.hasChanges {
do {
try Global_Context.save()
} catch {
let nserror = error as NSError
print("Error saving database context: \(nserror), \(nserror.userInfo)")
}
}
}
SECOND VIEW CONTROLLER:
import UIKit
import CoreData
class AddViewController: UIViewController {
#IBOutlet weak var itemEntry: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
itemEntry.delegate = self
}
#IBAction func addNewItem(_ sender: UIButton) {
let newDataItem = DataItem(context: Global_Context)
newDataItem.name = itemEntry.text
saveContext()
}
#IBAction func goBack(_ sender: UIButton) {
self.performSegue(withIdentifier: "toFirst", sender: self)
}
}
extension AddViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.endEditing(true)
return true
}
}
Main.storyboard:
Once you set up your view controller as a UITableViewDataSource (as in the example code), the table view should make things simpler by eliminating any need to manually manage individual Views.
Is this the functionality you were looking for? (Note about the example: it was set up in Xcode with "Use Core Data" enabled.)
Here is a link to the official documentation:
https://developer.apple.com/documentation/uikit/uitableview
What I have: the project, written on SWIFT5, which is an rss reader (I use standard XMLParser ). I fill cells with data from parser. In order to update the data in cells I implemented UIRefreshControl and wrote objc method, which contains the same method(fetchData - see in code), as I use to get data, but it doesn't work. Moreover, this method is called only once, when app is launched. When I close app and then open, data is not updated... How can I deal with it?
What I want: when refreshControl is activated, data in cells should be updated
What I did: I declared a variable called refreshControl, add it to tableView and wrote a method #refresh related to control
import UIKit
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var table: UITableView!
private let url = "my url"
private var rssItems: [RSSItem]? {
didSet {
DispatchQueue.main.async {
self.table.reloadData()
}
}
}
var refreshControl = UIRefreshControl()
#objc func refresh (sender: UIRefreshControl) {
fetchData()
sender.endRefreshing()
}
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(refresh(sender:)), for: .valueChanged)
self.table.refreshControl = refreshControl
table.addSubview(refreshControl)
fetchData()
}
private func fetchData() {
let feedParser = FeedParser()
feedParser.parseFeed(url: url) { (rssItems) in
self.rssItems = rssItems
DispatchQueue.main.async {
self.table.reloadData()
}
}
}
End refreshing once you get data and set or add refresh control
#objc func refresh (sender: UIRefreshControl) {
fetchData()
}
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(refresh(sender:)), for: .valueChanged)
self.table.refreshControl = refreshControl
// table.addSubview(refreshControl)
fetchData()
}
private func fetchData() {
let feedParser = FeedParser()
feedParser.parseFeed(url: url) { (rssItems) in
self.rssItems = rssItems // as you are reloading table here
DispatchQueue.main.async {
refreshControl.endRefreshing()
// self.table.reloadData()
}
}
}
I've been looking at and trying all the solutions others have posted to this problem, but I'm still not getting it.
My use case is very simple. I have a viewController with a button, when that button is pressed I want to navigate to another tab and reload the data including an api call.
When using the button, I navigate to the tab fine, but viewDidAppear is not being called.
If on another tab, and navigate using the tab bar, viewDidAppear works fine. Also viewWillAppear is working, but I have to add a manual delay to the functions I want to call so it's not ideal.
So what do I need to do to navigate using self.tabBarController.selectedIndex = 0 and get the functionality of viewDidAppear?
Update: The viewWillAppear method I added gets called but I have to add a delay to my functions in order for them to work, and it's a bit clunky, not ideal. Not sure why viewDidAppear will not work :(
Here is a screenshot of the structure:
I appreciate any help on this one!
The "current" ViewController is my tab index 2:
import UIKit
class PostPreviewVC: UIViewController {
//Here I create a post object and post it to the timeline with the below button
#IBAction func postButtonPressed(_ sender: Any) {
//create the post via Firebase api
self.tabBarController?.selectedIndex = 0
}
}
In my destination viewController:
import UIKit
import Firebase
import SDWebImage
import AVFoundation
class HomeVC: UIViewController {
// MARK: - PROPERTIES
var posts = [Post]()
let refreshControl = UIRefreshControl()
//more properties...
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
configureTableView()
reloadTimeline()
UserFirebase.timeline { (posts) in
self.posts = posts
self.tableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("viewDidAppear")
_ = self.view
setupUI()
configureTableView()
reloadTimeline()
UserFirebase.timeline { (posts) in
self.posts = posts
self.tableView.reloadData()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("viewWillAppear")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.reloadTimeline()
self.configureTableView()
}
}
//All the tableview code below here...
}
Added a custom class for my tab bar controller:
import UIKit
class TabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("viewDidAppear in tabBar custom Class called")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("viewWillAppear in tabBar custom Class called")
}
}
When you are using UITabBarController, the method viewDidLoad will called only once when any UIViewController is loaded in memory. After that, when you are navigating the same UIViewController, then it will load from memory.
In order to overcome this problem, you must divide your code in viewDidLoad & viewDidAppear. So, in viewDidLoad, you only put that code which you want to intialize once throughout the app such as adding UIView's or other things, while in viewDidAppear / viewWillAppear, you can make API calls or some functions which fetches dynamic data.
Finally, when you are calling self.tabBarController.selectedIndex = 0, it will call viewDidLoad only once and viewDidAppear / viewWillAppear every time when you are navigating that UIViewController.
Hope this helps to understand like how UITabBarController works.
For UITabBarController viewDidLoad only gets called once. and your viewWillAppear and viewDidAppear get called multiple times. you can either check if your viewWillAppear gets called or not. because your view will appear gets called before your viewDidAppear it's just like going through the reverse engineering process.
You can also add viewDidAppear method into your UITabBarController custom class. and call its superclass method into it in that way I think it will solve your problem.
Note: In the case of UITabbarController, Always do your UI update task and API calling a task in either
viewWillAppear or viewDidAppear
i have a UITableview that has unique cells,
each cell has it's own class and they have actions that i want to connect to my main UITableviewcontroller
I attach a protocol and open it in the tableviewcontroller
but it doesn't get read
how could I initialise it or what am I doing wrong ?
here is my cell class :
import UIKit
class AddFaxHeadlineTableViewCell: UITableViewCell {
var delegate: AddFaxHeadlineProtocol?
#IBOutlet weak var addButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
#IBAction func onAddFaxNumberPressed(_ sender: Any) {
delegate?.faxButtonPressed()
}
}
protocol AddFaxHeadlineProtocol{
func faxButtonPressed()
}
and in my tableviewcontroller I extend the protocol:
class SummaryMainTableViewController: UITableViewController, AddFaxHeadlineProtocol, AddEmailHeadlineProtocol {
but the function itself never gets read:
func faxButtonPressed() {
var indexToInsert = 0
for forIndex in 0..<sectionsData.count {
// render the tick mark each minute (60 times)
if (sectionsData[forIndex] == "addFaxHeadline") {
indexToInsert = forIndex + 1
}
}
sectionsData.insert("addNewFax", at: indexToInsert)
mainTableView.reloadData()
}
You need to call:
cell.delegate = self
In your cellForRowAtIndex method
This is the common mistake done in protocols and delegates to forget to call delegate.
Here are few examples you can check all have missing is calling delegate:-
Swift delegate beetween two VC without segue
Delegate seems to not be working, according to the console
How to present another view controller after dismiss from navigation controller in swift?
Another way to go inside the vc without protocols
let cell = ///
cell.addButton.tag = indexPath.row
cell.addButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
}
#objc func buttonTapped(_ sender:UIButton) {
print(sender.tag)
}
Check that you are doing this:
cell.delegate = self (It's required)
Then improve your line of code like below. Because you will not set delegate then by calling this delegate method directly will get crashed.
#IBAction func onAddFaxNumberPressed(_ sender: Any) {
if let delegateObject = delegate {
delegateObject.faxButtonPressed()
}
}
Second,
In this line, delegateObject.faxButtonPressed(), you will need to send some parameter to identify that will cell is clicked. So you can pass here button tag or you can pass cell also.
I have a collectionView controller with a collectionView populated by a database. In this controller I have reloadData() in 'viewDidAppear' and 'viewWillAppear' functions.
The collectionView controller has a modal segue to a gameViewController. After the 'game' is finished in the gameViewController, the database is updated (this works) and the modal is dismissed back to the collectionView controller.
func gameOver() {
self.dismissViewControllerAnimated(true, completion: nil)
}
However despite 'viewDidAppear' and 'viewWillAppear' being called (I confirm this with a println()) when the gameViewController is dismissed, the collectionView data does not reload. So the updated database data isn't shown in the collectionView.
The collectionView data does reload if I dismiss the collectionView controller, and then reopen it.
How do I ensure the collectionView controller - reloadData() - is called after dismissing the gameViewController?
Code:
class GameViewController: UIViewController {
var collectionView: LevelsCollectionViewController!
override func viewDidLoad() {
super.viewDidLoad()
// code not related to question - creates game scene
}
override func viewDidDisappear(animated: Bool) {
collectionView.reloadData()
}
func gameOver() {
self.dismissViewControllerAnimated(true, completion: nil)
}
class LevelsCollectionViewController: UICollectionViewController {
// code setting up cells
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
println("viewWillAppear")
self.collectionView!.reloadData()
self.collectionView!.setNeedsDisplay()
}
override func viewDidAppear(animated: Bool) {
super.viewWillAppear(animated)
println("viewDidAppear")
self.collectionView!.reloadData()
self.collectionView!.setNeedsDisplay()
}
}
UPDATE:
The println() viewWillAppear & viewDidAppear are printed in the console after the dismissViewController, but the collectonView data isn't reloaded.
The database that the collectionView is taken from is updated (done is another function called before gameOver().
I tried the method suggested below of having a reference to UIViewController and calling reloadData on that from viewDidDisappear, but nothing happens.
Actually that's what the completion argument is for
func gameOver() {
self.dismissViewControllerAnimated(true) { () -> Void in
self.collectionView.reloadData()
}
}
Check to make sure that your data source is being updated (a few println's in your viewDidAppear() will help) - in addition, try storing a reference to the collectionView controller and calling reloadData() on it in the viewWillDisappear() method of your gameViewController.
Some days I want to kick my own backside!
I 'stupidly' forgot to reload the temp array (levelProgress) that was holding the information taken from the database (held on array in appDelegate) in the collectionViewController.
Thus while the database and the array in appDelegate were being updated, the reloadData() was still working off the tempArray in the collectionViewController. Resetting this tempArray on viewDidAppear(), then reloadData() did the trick.
override func viewDidAppear(animated: Bool) {
super.viewWillAppear(animated)
levelProgress = appDelegate.levelProgress
self.collectionView!.reloadData()
self.collectionView!.setNeedsDisplay()
}
Think I'm going code blind