Why is the tableViewCell getting unselected on coming back to the ViewController? - ios

I have a tableView with custom cell in my ViewController, I select a cell and go to next ViewController.
However on coming back, the cells gets deselected automatically.
This is the first time I am seeing something like this.
I haven't written anything inside, viewDidAppear,viewWillAppear,viewDidDisappear or viewWillDisappear of the first ViewController that could do something like this.
I ran the code in simulator with slow animation to see what is happening.
While coming back, the cell is still selected but soon the selection goes away.
I put a break point inside setSelected:animated: method inside by customCell to backtrack whats calling it.
Here is what I see ...

this is a property of UITableViewController, set clearsSelectionOnViewWillAppear to NO

Related

UITableView Header Snaps to the Top of the Screen on Certain iPhones

Sometimes (but not all the time!) on iPhones (and iPhone simulators) I notice my UITableView header has this 'snapping' behavior that, when I try to drag down from the top of the screen it snaps back up instead of fluidly moving back up like a tableView normally behaves.
I'm wondering if anyone knows of this bug, what causes it, or how I can fix it? I feel like it might have something to do with UITableViewHeader but I'm not sure.
Unfortunately, I cannot share the code, but I don't believe it is something in the code. I manually commented on almost every line of the code and the problem persists!
Here was the problem for me and I'm pretty sure its a bug in XCode/Swift.
At first my navigation controller in the storyboard has this setting:
In my code I have the following method declared:
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let hideBar = (viewController == self)
navigationController.setNavigationBarHidden(hideBar, animated: animated)
}
Used to disable the navigation bar on the home screen. The combination of these two (if the method is declared on the home screen) causes a bug as shown above. I am able to repeat this bug in a new project.
The solution to this bug is to check the Show Navigation Bar box on the Navigation Controller
Since code is not provided for this question, I am answering based on my assumption.
Assumption 1 - Using manual layout.
If you have code in layoutSubviews(). Then you might want to check your calculation again. Make sure view frames are calculated one time in layoutSubview() method.
Assumption 2 - View animation is blocked/delayed by some other tasks on main thread.
Make sure to run no-UI/API code in background thread.
Reuse cell instances: for specific type of cell you should have only one instance, no more.
Don’t bind data at cellForRowAtIndexPath: method ‘cause at this time cell is not displayed yet. Instead use tableView:willDisplayCell:forRowAtIndexPath: method in the delegate of UITableView.
Hey, can you move your header view content in first cell of first section, so that you can avoid header view problem. Then check if snappy problem is occurring.

ios 8 tableview reloads automatically when view appears after pop

I have a table view, onclick of 7th row, I push another view. Then when I come back using pop, tableview is reloaded automatically in ios8. It does not happen is ios 7.
Problem is cellForRow and HeightForRow for 0,1,2,3 cell is not called. Hence table scrolls the 7th row up, and not visible. 9,10,11 cells are visible.
I want table to stay as it was when I come back to that view.
I saved selectedIndex and scrolled table to particular index in viewWillAppear.
[self.table_exhibitorProfile scrollToRowAtIndexPath:self.selectedIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
It shows 7th cell, but its position is not same as it was. It goes to top.
It shouldn't be reloading unless you tell it to. It's not automatically reloading in iOS 8 for me.
The only possibilities that I can come up with are that
you're using CoreData, an NSFetchedResutsController, and the same managedObjectContext in both view controllers. If something changes in the managedObjectContext in the second view controller, when you pop back, the fetchedResultsController will reload the table, or
you're calling tableView.reloadData() in viewWillAppear() or viewDidAppear().
I had the same problem and this is how I fixed it:
According to the Apple docs:
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.
In my case I was setting the table view's dataSource to nil in viewWillDisappear: and setting it back to self in viewWillAppear: thus causing the data to be reloaded.
The 'automatic' reload went away when I moved the dataSource assignment to viewDidLoad
Hope this helps!

UICollectionView didn't show its data as loaded in another UIViewController

I have two UIViewController. One is ViewController, and another is GameplayViewController.
ViewController will execute
let gameplayViewCtrl = GameplayViewController()
self.presentViewController(gameplayViewCtrl, animated: true, completion: nil)
in viewDidAppear() function. So it shows then load another UIViewController.
GameplayViewController has UICollectionView as its member. It extends/conforms to UIViewController, UICollectionViewDelegate, UICollectionViewDataSource. I've linked delegate, datasource, and UICollectionView variable as #IBOutlet properly in storyboard.
With above setup, I tested it. It shows white screen for a sec, then shows black screen. The cells in UICollectionView in GameplayViewController are never get loaded (no call in code checked via breakpoint). But if I changed the entry point (an arrow in storyboard) to point at GameplayViewController, then it loads properly and show all data in cells. Things work fine.
What's the problem here? I guess it's about event chain that never get caught in non-default UIViewController.
FYI: I do this in xcode 6 beta 3
Updated
I did some more research, and found out there're at least two possible ways to do this. One is my approach, and another is to use segue.
Segue works, you can find how to do it How do I use performSegueWithIdentifier: sender:?. I tested it.
I also tested to switch from GameplayViewController to ViewController (reverse from my original test). It showed black screen similar to the first test. But it should show white screen as for ViewController, it has white background color. This triggers me to think that something's wrong creeping inside.
For now, I use segue approach. But I still want to know why self.presentViewController() won't do the job for us, and it shows black screen and nothing called or happened.

ViewController loses its properties when I go back

I have two ViewControllers. The first has UITableview that I push data from to the second ViewController. Whenever I go back, the first ViewController loses its properties - Its navigationbar background disappears and for example the sidebar menu does not work either. Is there any way I could reload it while pushing the back button?
Thank you
Sounds like you are probably doing your setup in viewWillAppear instead of viewWillLoad. This means that when the view will appear on return, the setup is happening again and perhaps leaving you in an unexpected state. Put breakpoints in your view controller delegate methods and see what order things are being called in and why.

UITableView reloadData best practice

When is the best time to call [reloadData] on a UITableView? viewDidAppear or viewWillAppear? If a view is unloaded, will the underlying tableview be unloaded as well?
The reason I am asking is due to some behavior I am seeing. Let's say a view is asked to reload its datasource, but is deallocated before it can finish. A scenario would be with a UITabBarController and navigating to a different view than the ViewController being selected. So what happens is that viewWillAppear gets called, but viewDidAppear does not (since I navigated away).
As a result, the ViewController gets deallocated (along with its model data), but if I am calling reloadData in viewWillAppear, the "cellForRow", and "numberOfRows" methods are invoked which causes a "deallocated instance" error. Does that make sense? Do you normally put in code that checks for nil if the model can be changed anytime the view is requested to appear?
I would load the table once the view has loaded, it allows for all of the UIObjects to be created before messing with them. Not sure, but I believe the table is automatically unloaded with the superview.
Take a look at this question>>>
Objective C - Correct way to empty and reload UITableViewController with NSMutableArray
before you get the datasource from server or anywhere, you can display the tableview with a blankcell(a cell that you can draw sth like 404 web page) , when you get the data, then reload the tableview.
remember to set number of cell to 1 for the blankcell

Resources