CollectionView nested inside Collectionview - ios

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/

Related

Move CollectionViewCell to another section

I have a UIViewController with a collectionView inside it. The layout may seem confusing but I am making use of horizontal scrolling UICollectionViewCells, presented in 3 sections.
The main collection view is made up of 3 sections. I access 3 different UICollectionViewCell classes for each section becuase it is fetching different data for each one. Once the data is fetched, it dequeues another Cell class, which is the same one accessed for all the sections.
There are 2 buttons in each cell. When I press the button, a value changes within the database. When I re-run the program, the cell in which the button was pressed has moved to its corresponding section that it should be in as per the change. However, I want to achieve this as soon as I press the button. How can I move the cell from one section to another, baring in mind that they the cells presented are inside a collectionview cell making up the sections, which is then inside a greater UICollectionView.?
I've tried researching how to do this but no luck, and everyone seems to be using a drag and drop method which seems too complicated for what I am trying to achieve. Would appreciate any help.
Thanks
The approach I followed to achieve the above situation is using a custom Delegation when cell is removing from collectionviews. A container CollectionView has two sections with 2 different cells containing two different Horizontal UICollectionViews. These two collectionview's cells have UIButtons. When tap on one collectionView's button, that particular cell is removed, custom Delegate method calls and another CollectionView's cell is populated. This is the link of the project I have created for this particular scenario. For better understanding, here is a GIF for demonstration.

How can I Create a Tableview Under Custom Tableview cell

I am just thinking. Suppose, i have a tableview which have custom cell. It's simple. But My idea is that, when i click a tableview cell then another tableview is appear under that tableview cell, and again i click that cell then that sub tableview disappear. Similarly when i click second cell than work same. Is it possible? Please Provide me any idea or reference.
This is entirely possible, you're talking about Expandable cells.
My example here
The general idea is that your custom cell has a tableview at the bottom of the cell, and what you do is just change the cell height to display said tableview, on tap.
It's not easy, I'm not gonna lie it took us a while to do it, but we managed, and I'm telling you, it's very possible.
You can find a lot of help using the Expandable Cell keywords.
Note that you're gonna find yourself handling a lot :
What to do when the expanding cells is shown off screen?
What to do when you're expanding the first/last cells ?
What to do when expanding another cell ?
What to do when scrolling inside that cell (a scrollview inside a scrollview !)
There are many cases where it'll work, but won't work fine, and there is gonna be a lot of fine tuning. Specially in our case where we have rounded corners, but only when the cell is expanded, and not in cases where it's the last or first cell (next to section header).
They look cool and make you feel proud, but don't say to your PM it'll be done in a week, because it's a pain to build.
If you want to show additional cell information, you can add more cells after the cell indexpath you have clicked.
Create a custom table view cell classCustomTableViewCell by subclassingUITableViewCell class. And system will generate CustomTableViewCell.h, CustomTableViewCell.m, CustomTableViewCell.xib files for you.
Add protocols UITableViewDataSource and UITableViewDelegate in your CustomTableViewCell.h and implement the required methods in CustomTableViewCell.m files
Add a method for setting datasource and use the datasource for updating the table.
NOTE:
Handle table-dequeue mechanism properly, otherwise you will end up
with weird issues that may take time to investigate and resolve.
If you use this custom cell for all the cells in your parent table then the gestures will only listened by the child table. So plan for that too.
Please visit my blog for the sample code. https://myioslearnings.blogspot.in/2017/03/nested-table-view-in-ios-objective-c.html

UITableView inside UITableViewCell - didSelectRowAtIndexPath

I am making an interface which have UITableView with four custom UITableViewCell's. And every other UITableViewCell have also UITableView. This mean I have TableView inside a TableView.
Let me call first tableView - ParentTableView and nested tableView - ChildTableView. So I implemented method didSelectRowAtIndexPath on both tableView's. But when the app is running, only the method of the ChildTableView is being called. I need to know inside the ParentTableView, which cell is being tapped.
How can I transfer that information further from ChildTableView to ParentTableView.
This may be a silly question, but I can not find any reliable solution so far, so please help me.
Thank You in advance, kind Sir
First, I think nested table views is a bad idea. But I don't know your use case, so it might be an exception.
The table view controller class used inside a cell have its own #protocol definition and set the outer table view as its delegate. In the inner didSelectRowAtIndexPath: it can inform the outer table view about the selected indexPath, its own indexPath and any other information you might want to transmit.
Try using collection views. You can do a layout that works like a regular table view and then in the cells that need to have a table, you can make those separate cells with another collection view inside or a table view inside of it.
As mentioned before, it's not easy doing table views inside of cells, and it can be very tricky to get things to work correctly.
This is an old tutorial I wrote which may help and be of guidance. But since then there's been collection views and auto layout, so keep in mind it's very old.

UICollectionview and/or UITableview cells not selectable until after view scrolled?

For every collectionview and tableview within my app, none of them are recognising the selection of a cell unit AFTER the table or collection has been scrolled a little?
This only happens within this current app, no different methods have been used, no collection/table subclassing etc. All collection views and table views are implemented with all relevant dataSource and Delegates hooked up and implemented etc.
As this is a common thing will all my tables, I'm wondering if it is a setting within the project that is causing this.
If anyone has experienced and previously solved this issue, I'd appreciate any help.
Salient points that may be relevant -
-All collecitonviews / tableviews are set up on storyboard.
-All cells are added directly to table/collection within storyboard and cell view configured using their own class files.
-Deployment target 7.0 for iPhone only
-All tables/collection cells are loading fine, everything looks fine with no crashes... just that the tables need 'woken up' so to speak?
Thanks in advance,
Jim
I've had similar issue, and I fixed it by subclassing UICollectionViewCell/UITableViewCell and setting selected property to NO in prepareForReuse method.
- (void)prepareForReuse
{
self.selected = NO;
}

Embedding a UIScrollView with a UITableView

So Path uses this type of page where there is a view above their customized looking table that is a background photo, which contains some user info among other things. I'm trying to recreate something very similar to this.
So lets say that I hypothetically wanted to make a view that shows exactly the way the Path app does, but instead of that weird customized version of a table view that they have, there is an actual table. How would I do something like this? The reason why I would need there to be another UIScrollView embedded into the view is because the entire thing needs the capability to scroll. I'm trying to be as detailed as possible, but its a little difficult to explain.
What I'm imagining is going to happen if I just tried it right now, is that I'd embed a UIView above a UITableView within a UIScrollView that's the size of the frame, and when I'd go to scroll, the user would only scroll the UITableView, and not the entire thing at once. Hopefully that helps convey my doubts.
Another possibility is that I'm totally over thinking this, and I can simply just subclass a view in the header of a UITableView and it would stretch the width and height that I'd like. Hopefully this is the way as this would be easy!
Anyways, can anybody weigh in on this?
Path just uses a normal UITableView with UITableViewStyleGrouped.
The custom view at the top is the header of the first section of the table.
They also access the UIScrollViewDelegate method of the UITableView to change the look of the view (I think the image is moved) when the scroll view scrolls.
If you'd like a tableview that only scrolls within a part of the view and other stuff above it then you need to use a UIViewController. Then you can make it conform to UITableViewDelegate and UITableViewDatasource and add a UITableView and make the view controller the datasource and delegate.
Then you can also add a UIScrollView to it as well.

Resources