I'm having a difficult time understanding what I should even be looking for to debug this issue.
I have a UITableView that populates correctly.
I select one of the UITableViewCells which upon selection presents a new view to the user with more detail.
If I click Back on this new window, the original UITableView shows none of the UITableViewCells anymore.
I guess the problem is when you are coming back, your array is being empty somehow. In your first "ViewController" override the "viewWillAppear()" method and print the count of your array in this method. Then load the app and check the print result, then go to second "ViewController" and press the back button and see the print results again. if it shows zero "0" then check your code and try to find out where your array is being empty.
Hope this will help.
I ended up setting the delegate and datasource in the viewDidAppear() rather than the viewDidLoad(). Once I did that, I added a tableView.reloadData() inside of the completion handler for retrieving the data from the API and the tableView was then being populated as expected.
Related
I've read 3 pages of google results and none worked for me, here's detailed description of the problem:
Every time I launch my app If I tap on the tab bar item that shows the vc that contains my table view it is empty. I have to go to another tab and return to it, just then my table view fetches. Spent 8h trying to fix it already.
Same exact VC shown with a button tap is displayed correctly and loads every time.
What i've already tried:
triple checked if I have:
tableView.delegate = self
tableView.dataSource = self
Tried moving the fetching to viewDidAppear or viewWillAppear, none worked.
Diagnosed it, number of sections is returned but number of rows = 0, cellForRowAt never executes on first tab bar click, even tho my posts are fetched correctly.
Tried fixing any constraints that exist, didn't work.
It simply seems like tableView.reloadData() doesn't work. I surrender, please help me!
You still haven’t provided a very clear picture. At a glance, though, I’d say you need to add a call to your table view’s reloadData() method inside the braces on your call to posts(for:completion:). (I’m guessing about the exact name of that function since you don’t include it.)
If your posts(for:completion:) invokes its completion handler on a background thread then you’ll need to wrap your call to reloadData() in a call to DispatchQueue.main.async() (Since TableView.reloadData() is a UIKit call and you can’t do UIKit calls on a background thread.)
The code might look like this:
StartTestingViewController.posts(for: UUID as! String) { (posts) in
DispatchQueue.main.async {
self.posts = posts
self.tableView.reloadData()
}
}
(I didn’t copy your whole viewDidLoad method, just the bit that calls your function that fetches posts.)
The UITableView.reloadData() method tells your table view “The number of sections/rows of data or their contents may have changed. Please re-ask your data source for contents and re-build your cells.” The table view will ask for the number of sections again, ask for the number of rows in each section, and ask for enough cells to fill the table view’s frame with cells.
If you don’t call reloadData() your table view has no idea that there is new data so it doesn’t reload itself.
I suspect that the reason that you can go another tab and come back and see your data is that the data has been loaded while you were on another tab, and so is available immediately. I’d have to see the exact details of your tab displaying code to be sure.
Okay, so I have this table view, where each row has a label with a string. I want to make it so that if I select a row, the string of that row will be carried to a label of another view controller. I tried using delegate and protocol, but I always keep on getting a nil value for the delegate. The string is not the problem, nor is the table view. It can print the value of the string in the row, but whenever I try to use a delegate, it simply won't work. I added the protocol, established it in the other VC, set it as a delegate, and added the value of the variables in the table view whenever a row is selected. Any suggestion? This is really bothering me.
I was trying to do this for myself like 5 minutes ago and it worked. The thing i was trying to do is to add a string that i got from one ViewController to an array in another ViewController. It printed in the console nil for me because the array was an optional. But as soon as i removed that question mark and initialized it as an empty array it worked like a clock. And i also wanted to show it in a collectionView, but i forgot to add this
Dispatch.main.async{
self.collectionView?.reloadData()
}
I also used this guide
http://swiftdeveloperblog.com/pass-information-back-to-the-previous-view-controller/
Hope i helped.
I'm very new to swift programming. I have been playing around with this for a while, but I am not getting anywhere so asking here.
I have a tableview which I can load the data into the view from CoreData no problem. I have an ADD button at the top that segue's to a new tableview, with a long list of options they can pick from. This tableview also works fine, and includes a search bar.
When the user selects an item row from the second tableview, it inserts that item into CoreData and segue's back to the first tableview. This is where my problem is, the data does NOT update on the visible view.
I call tableview.reloaddata() and I can see my code calling the fetchedResultsController with the new query that would return with the new data. But the code never gets to the cellForRowAtIndexPath func so therefore the visible data view never changes. It remains the same display that was visible when the add button was pressed.
How does the visible data get updated? What am I missing?
When using an NSFetchedResultsController, if you want it to "automatically" update the contents of the tableview then there are a couple of things you need to make sure of...
You have to become the NSFetchedResultsControllerDelegate and implements the methods necessary for the updating of the table view. These are quite lengthy and can be found on the Ray Wenderlich website. The code on here is in Objective-C but it's fairly easy to convert to Swift.
The second thing you need is to make sure that the core data update is done on a background thread. Again the website linked above shows this.
Once you've done that then you don't actually need to run [tableview reloadData] because the fetched results controller methods will manage everything for you.
A simple tableviewController, empty. A modal that can be launched from a button. No data in the data source for the tableview, and no rows displayed.
I open the modal, use it to add an item, and return to the tableview controller. The tableview updates itself automatically, and the new row is displayed.
I add a second item. The table view does NOT update automatically.
I can tell by logging inside numberOfSectionsInTableView that even if I go to add the first item and cancel, the tableview refreshes - it asks for the number of sections, rows, and the cell to display (if there is one). But not if there is one pre-existing row in the table.
I've scoured my code but can't find anything that would cause the tableview to know when the first item is added, but none afterwards.
Any ideas?
EDIT - I have further narrowed my issue so will close this shortly and have posted a more specific question at Why does an empty tableView check the number of sections but a non-empty one does not?
from Apple's documentation:
UITableView overrides the layoutSubviews method of UIView so that it
calls reloadData only when you create a new instance of UITableView or
when you assign a new data source.
maybe you are setting data source of table view after showing that modal? So at the second time data source does not changes and tableView does not update.
another key (I'm not sure about that) may be the showing of the modal first time. The method -addSubview for modal is causing -layoutSubviews.
I have a TabBarController with two tabs SCHEDULE and DATE RANGE
When the view loads I'm hitting a webservice to collect the end users Schedule for that day. After parsing the XML I call ScheduleTable reloadData and all is well. I have tons of NSLog statements to follow the performance.
Clicking the DATE RANGE tab displays a DatePicker. After selecting the date range I have a button which when clicked passes the dates to the webservice again. In my log I can see the new dataset and can verify I have the data I need, when the code reaches the reloadData line, it does not fire the UITableView reload methods.
Any help is appreciated. I've tried viewWillAppear and self.ScheduleTable reloadData, neither have helped.
Check to see if you've connected the UITableView to the ScheduleTable IBOutlet in Interface Builder.
You say that the first refresh is working. That may be because it is automatically done by the tableview.
I understand that the second time you are calling reloadData programatically. If you haven't connected the IBOutlet, then your ScheduleTable variable is nil and [ScheduleTable reloadData] will do nothing.
when the code reaches the reloadData line, it does not fire the UITableView reload methods. Any help is appreciated. I've tried viewWillAppear and self.ScheduleTable reloadData
Just before saying [self.scheduleTable reloadData] (never start an ordinary variable name with a capital letter), log the table:
NSLog(#"%#", self.scheduleTable);
I'm betting it won't be the table and that you're accidentally sending this message into empty air.