Xcode: Collection view not displaying properly - xcode

I was working on app that would take some input from the user and put them into a collection view. The data goes to the database and retrieves successfully into a dictionary movies, but while displaying the collection view, it shows doesn't show the third item, but shows the second item twice. After re-running the app, the problem gets fixed. How do I fix this without having to re-run my app every time?
movieCollectionView is my UICollectionView, and MovieCollectionViewCell is my Cocoa Touch Class file for the collection view.

Fix for the issue - Add following call inside refreshBtn(_ sender: Any) function.
productCollectionView.reloadData()
Other observation - You shouldn't need this line -
productCollectionView.collectionViewLayout = UICollectionViewFlowLayout()

Related

How to reuse WKInterfaceTable in WatchKit

I chose page-based layout for my WatchKit app. Each page shows a table. I want to keep the layout of the table exactly the same across all pages. I will be feeding filtered data to each page at run time, so essentially pages will be identical, except the data will be different.
One way to achieve this is to manually create InterfaceController instance for each page in InterfaceBuilder, then populate with GUI elements and connect the outlets. The problem here is whenever I want to change something (say, move a label or add a button), I will have to apply that change consistently to every page.
Moreover, for each page I have to connect the outlets to table row controller, essentially repeating myself over and over again. Here's an illustration:
Is there a way to reuse a table?
I considered inheritance, but the documentation tells me not to subclass WKInterfaceTable. This also rules out creating a table programmatically.
You should create a single interface controller with the table and various row types. Then you want to create an architecture very similar to my answer here. You'll only have a single PageOneInterfaceController which should be named TableInterfaceController in this example with TableInterfaceIdentifier as the identifier. Then you would do the following:
MainInterfaceController.swift
func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
let context1 = "context 1 - for you to fill out"
let context2 = "context 2 - for you to fill out"
let context3 = "context 3 - for you to fill out"
WKInterfaceController.reloadRootControllersWithNames(
["TableInterfaceIdentifier", "TableInterfaceIdentifier", "TableInterfaceIdentifier"],
contexts: [context1, context2, context3]
)
}
This will reload the page set using the same interface controller that displays all the data in the respective context.
To accomplish this, provide the same WKInterfaceController in reloadRootControllersWithNames:contexts: multiple times, but provide a different context to each one.

Xcode hanging, runs out of memory when using Bindings and IB

I have a core data model with two entities, for the sake of this post I will call them Category and Items, Category is the parent entity with a one-to-many relationship to Items.
In IB I have a NSTreeController and NSOutlineView that manages the Category entity, this works great.
I also have an ArrayController with Mode set to Entity, Entity Name set to Items, Content Set bindings set to Tree Controller with Controller Key of "selection" and Model Key Path set to the relationship.
I have tested all this works by selecting Categories in the outline view and outputting how many Items are available inside the array controller when I change selection from the tree controller using the following code:
SWIFT:
#IBOutlet var itemsArrayController: NSArrayController!
#IBAction func logResult(sender: AnyObject) {
println("Items Array has \(itemsArrayController.content?.count) records")
// Print first item to console
var anItem : Items! = itemsArrayController.content?.objectAtIndex(0) as Items
if let something : Items = anItem? {
println(something.name)
}
}
Now I want to hook up a Table View to the Array Controller that will list all the Items of the selected Category.
So If I bind Table View Cell's Value to Items Array Controller.arrangedObjects.name and click RUN xcode starts compiling and just doesnt proceed past 50%. Activity Monitor->Memory shows "ibtooid" with 5GB of memory after awhile before the entire system locks up and I have to hard reset my mac. Even if I click stop in Xcode the ibtooid continues to eat memory, The only way to stop from a hard reset is to kill that process,.
Any help would be much appreciated.
Thanks
Xcode Version 6.1 (6A1046a)
Fixed it by changing the table to Cell based rather than View Based. Will file a bug with Apple.

Switching from one view controller to another automatically

I have a project where a user fills in a web form, once the user hits submit it moves to a viewcontroller that shows a message "sent" or "error".
How can I make it automatically move to the main view controller without pressing a button, after a certain amount of seconds (so it displays the message "sent" for approx 2 - 3 seconds then moves to the main vc)
Any information would be highly appreciated, thankyou
Sean
You should be able to show the view and, in the viewDidLoad method (or viewDidAppear), set a timer to call a method that dismisses the view controller. Use performSelector:withObject:afterDelay to perform the delay.
Have a look here:
How do you trigger a block after a delay, like -performSelector:withObject:afterDelay:?
You can trigger some code inside a block to run after a specified delay. Inside that code you can include your code to navigate to the other view controller.
The code for doing this depends on whether you are using storyboards or not. If you are using storyboards, you can use:
[self performSegueWithIdentifier:#"MySegueName" sender:self];
If you are not using storyboards, you can use the following to display the second view controller modally:
[self presentModalViewController:myNewViewController animated:YES];

Xcode storyboard seque loops back to the same view

I am using storyboard seques to change views to other parts of my app but when I linked my main view to the next and called for the seque in the function within my enter button it just loops back to my main view and refreshes the app.
Here is my function call:
[self performSegueWithIdentifier:#"Enter" sender:self];
In the storyboard I have my main view linked to the next view with the identifier "Enter" but I still get send back to the same view. Is there any way to specify the destination of the next view? Do I need a certain variable from the other view controllers the need to be imported to the code of the main view controller?
I can provide more code or information if needed.
Picture: http://i1180.photobucket.com/albums/x411/Ryute88/screen.jpg
Identity Inspector Picture: http://i1180.photobucket.com/albums/x411/Ryute88/screen2.jpg
Picture 3: http://i1180.photobucket.com/albums/x411/Ryute88/screen3.jpg
Thanks in advance.
Please check this site: All about storyboarding
You will find details on doing your stuff.Try that and check if the problem still exists!
Are you using the write segue to call on the next view e.g. pop can only be used in case of Navigation Controllers.
In any case the problem will be trivial because storyboards do everything for you.

Where does the program return to when one view calls a 2nd view and the 2nd view closes?

I've been working all day on that classic problem of passing variable values between views. (I've read and typed in almost every example I've found in my books and on the net!)
My second view is a picker, and I have to retrieve the row value and send it back to the calling program. I've managed to pass data from the first view to the picker's view, but what method runs in the first view controller when the 2nd view controller closes? That is, where do I put the code to receive the picker's value?
Incidentally, I settled on the Shared Instances method of passing the values, found at http://www.cocoanetics.com/2009/05/the-death-of-global-variables/
Thanks,
-Rob
I found an answer to my question in Sams Teach Yourself iPad in 24 Hours. The code goes into the viewWillAppear method. It's like ViewDidLoad but it's for returning to the View from a different View. If you are updating a label, for example, on the first view with data set by the second view, the data being passed is set into the label in viewWillAppear.

Resources