Add collectionView to Header - ios

I'm trying to add a collectionView to my CollectionView header. A collectionView inside a collectionView I guess. I've got the header setup and I get a blank collectionView in the header part when I run the app but where can I programme this collectionView.
I've got an outlet for it created in the header.swift file which I can access in my main collectionView file but I can't configure the cells, set the number of sections etc. If it was just a label in the header I could set the text etc here but I'm not sure how to configure a collectionView here.
I tried creating a CollectionViewController file and just linking that to the colectionView in the header but that doesn't work. How can I create a subclass for that collectionView (in the header)?
Swift 3

Okay heres the deal: I guess you use a UICollectionViewController or a UIViewController with a UICollectionView which implements the UICollectionViewDataSource protocol, right? This will massively and unnecessarily bloat your UIViewController.
A better way would be to create a separate object to implment the DataSource of your collection views. One for your main collection view and one for the header collection view. Then you just have to wire them up. You must keep a reference to your data source objects somewhere.
So usually, in viewDidLoad of your view controller you get your collection view and call collectionView.delegate = theDataSourceObject.
theDataSourceObject could be a lazy var in your UIViewController lazy var myDataSourceObject = MyDataSource()
Then, wherever you create your header collection view, you also set its datasource. I hope you understand what I mean and this answer helps you. If you should learn one thing from it than that your view controllers don't necessarily have to be the datasource and/or the delegate of your UITableView or UICollectionView.
They can be totally different objects. A singleton would be an option as well as storing instance variables of your datasource.

Related

I need help knowing where to start on designing a tableview or if a tableview is what I need

As in title, I don't know where to start with creating a view like this. Do I hard code it or use XCode editor? Is a table view what I need?
Yes, a UITableView is what you needed.
You need to create a TableView Controller.
Follow by creating a TableViewCell
Then you need to start coding. Hook up the delegate and data source methods.
For more information, check this tutorial by Apple:
https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/
Here you have a tableView with section header indicating the date of the matches, the tableView has a custom designed cell, each cell contains the names of the two competing team.
start by adding the tableView to your viewController using storyboards, with the delegate and datasource connected.
the sub class from UITableViewCell and create your custom cell with an xib file.

Migrate UITableViewController to UITableView

I currently have a UITableViewController, but would like to add a label at the bottom of the screen. I think the easiest way to do this would be to just use a UIViewController with a UITableView. Is there an easy way to migrate over? I've noticed some functionally is different; for example UITableViewController has a numberOfSections method, whereas UITableView does not have that method. Also is it possible to use a UIViewController as the delegate for a UITableView? Thanks in advance.
Update: I have attached a picture of the code, and the error given when trying to implement UITableViewDataSource.
You can do this. You need to implement the UITableViewDelegate and include the UITableViewDatasource in your View Controller, then you can use the functions you were using in your TableViewController.
Basically what you should do is add a tableview in the interface builder, or programmatically (depending on your choice). If it's done through the interface builder then outlet it to your Controller. Add the Delegate to your class, and you would have access to the tableViewFunctions (cellForRow, numberOfItems, numberOfSections, didSelectRow etc...).
Also, when you create the tableview specify the tableview.delegate = self and tableview.datasource = self.
As far as I know, the methods are the same, you just won't need the override bit before each one if its just a UITableView. You can use UIViewController as the delegate, yes, by setting myTableView.delegate = self in its viewDidLoad method.
If its just a label you need however, you might consider adding one programatically? Similar results achieved with a no data label for example - see here. You could then play around with the positioning.
This error is thrown because you need to set dataSource to your TableView Object like self.tableView.dataSource = self. After this you can use freely all methods for UITableView including numberOfSections as you mentioned.

CollectionView nested inside Collectionview

I have seen solutions where a collection view is nested inside a table view but for my app I need to have 2 collection views as it makes it easier to do some other things.
So lets call the root collection view VerticalCollectionView which only scrolls vertically and the nested collection view HorizontalCollectionView which only scrolls horizontally. I created them using the Storyboard. Below you'll see the orange is the Vertical with the green Horizontal with a label inside it.
And I have set the delegate & datasource of both collections to the same CollectionViewController.
I distinguish between the 2 different cells by checking which tableview the delegate method is referencing as such
My problem is that HorizontalCollectionView isn't getting instantiated. I have everything working for VerticalCollectionView, the background color, the number of items, etc. In the VerticalCollectionViewCell, I have an IBOutlet referencing HorizontalCollectionView
I have used nested collectionViews in my recent app a lot. at first it didn't work for me but when I learned its away it became pretty simple.
instead of setting the HorizontalCollectionView delegate and dataSource to the same CollectionViewController make a UICollectionViewCell and set the delegates and dataSource in awakeFromNib() function of the cell and write the HorizontalCollectionView functions in that cell. it works like a charm ;)
if you needed more detailed answer let me know.
In case someone is looking here since WWDC 19, Apple has introduced CompositionalLayout for UICollectionViews which make these tasks much much easier.
One should read here:
https://developer.apple.com/videos/play/wwdc2019/215/

Register multiple header supplementary view in the same UICollectionView through storyboards

So I have a UICollectionView that I want to use to display multiple UICollectionViewCells and different headers depending on the state the user is in. The problem is I can't seem to register multiple UICollectionResuableView (header) views within the storyboard. If I try to register one as a footer and use it as a header the app crashes. I also do not see anywhere in the storyboard to declare the number of sections, which would alleviate the problem. For now I'm having to use multiple UICollectionViews and hide/unhide the collectionview I want to display based on the header I need. Is there a better way to do this, or a way to register multiple header views in the same UICollectionView within the storyboard?
You cannot register more than one header in a UICollectionView in a storyboard, but you can register an arbitrary number of them in code. You should create however many headers as you want in nib files (One nib per header, with the header as the top level object), and register them with your collection view instance in viewDidLoad.
You would do this with the registerNib:forSupplementaryViewOfKind:withReuseIdentifier: method.

Why are my static prototype UITableViewCells from Storyboard showing up blank?

I defined the static prototype UITableCells, subclasses, outlets, etc. in the storyboard, but when I run the app and go to that viewcontroller/scene, the cells are there, but they have no content?
When you set up a table view with static prototype cells in Storyboards, you do so by having dragged out a UITableViewController and then subclassing it.
The trouble is that UITableViewController is oh so helpful and supplies datasource methods for you to override.
Force of habit had me getting in there and supplying default implementations for those datasource methods.
Turns out that not only do you NOT have to supply datasource methods, but if you do, they interfere with proper display of static prototype cells. I just commented out all of the datasource methods in my viewcontroller and everything started displaying fine.

Resources