Is it possible to keep a collection view inside another collection view? - ios

I have a scenario where if we click or select any cell inside a collection view which is in vertical scrolling, it should display a single row horizontal scrolling below the selected, this should applied for all the cells, so I have created a collection view inside the view controller, and I thought of creating another collection view inside the method:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{}
Because here not only collection view is creating, but also the next cells are moving down. Hope you understand the situation. But I was struck here.
I'm not getting any idea how to write code here,
1) Is my approach is correct,
2) If yes, give me some instructions how to achieve this?
3) If I insert a cell, it will insert a cell but it should be horizontal scroll as shown in the image i posted. so Help me out ??

One way to achieve your design would be:
Add two type of UIcollectionviewcells to collection view
cell1: cell1 is the normal cell on which you click.
cell2: You will insert this cell at indexpath + 1 in collectionview when tapped on normal cell
Add a scroll view to cell2. with horizontal scrolling or collectionview.
cell2 will be the datasource and will contains logic for that view.
This will make a lot easy for handling events.

Related

UITableView + UICollectionView + UITableView sizing issue

Below attached image is representing my requirement:
In this image the yellow color UITableView will have list of different UITableViewCell items. In one of the UITableViewCell it has UICollectionView with its list of UICollectionViewCell items and the scroll direction for the UICollectionView is set to horizontal
Each UICollectionViewCell will have one UITableView (Highlighted in Red Color) and it will have its list of UITableViewCell (Highlited in Rose Color) items. Here, the UITableViewCell (Highlited in Rose Color) item can collapse/expand. Whenever the cell is collapse/expand the red table's content size is handled automatically. But, whenever the red table's content size is increased the collectionview size is not increasing.
I have added all relevant constraints to these UI elements. But, the content size or frame of the collectionview is not expanding when a tableview cell is expanded.
To understand better here i am breaking it down how the views are designed:
Yellow tableview embedded inside a UIViewController and it will have one simple UITableViewCell which has no custom class
To the cell programatically the UICollectionView will be added. This UICollectionView is placed in one xib file and it has embeded inside a UIView
UICollectionViewCell has been created in one xib and it will have UITableView element inside.
Also, UITableViewCell has its own xib to refer and this is the cell which will gets expand/collapse
I have tried modifying the intrinsicContentSize property to modify the UICollectionView property's height where it has embedded inside UIView element whenever the expand is happening. Please, refer the CardView.swift for the reference.
CardView.swift
I know the design is bit complex. But, what am i missing here?
Based on your requirement in the image, I just created a sample project here.
I did not use separate Xibs. However, I was able to achieve what you asked for.
You might have to handle the reload of the innermost tableView better based on your apps business logic.
Here's what I have implemented:
The top most view controller has a parent table view.
This table view cell consists of a child collection view.
The child collection view cell consists of a grand child table view.
The grand child table view is the one corresponding to the table view Highlited in Rose Color in the question.
To answer your question about the collection view not expanding: to expand the child collection view when the grandchild table view cell is tapped, I am reloading the parent table view by increasing the row height for the cell containing the collection view. Hence you don't have to worry about adjusting the constraints in auto layout
In case you want to add additional content into the child collection view cell, you can add a height constraint to the grand child table view with a low priority and modify that whenever you need to expand the content in the grand child table view.
if you want to achieve point 6, as suggested by #Ramy Al Zuhouri, you might have to call: collectionView.performBatchUpdates before collectionView.reloadData() to expand the cell size
Refer to the video below for better understanding
This doesn't necessarily solve your problem, but sometimes it's a matter of forcing the collection view to invalidate its layout and recalculating its size. I once received an answer to a TSI from an Apple enginner:
So this is old fashioned way of forcing a re-usable container (Collection || Table) to invalidate its layout. You may even be able to just call invalidate layout. There are oddities in the system with AutoLayout and animations that sometimes throw the system into an odd state. If we do the above the container and can invalidate its layout and make updates without need to re-calculate + incorporate animations. To summarize, calling performBatchUpdates with nil parameters can be useful if you need to force an update based on layout content (not necessarily datasource changes).
It looks like a hack to be used with caution, but it can be worth to give it a try. Whenever you need to recalculate the size of the collection view's cell to expand or shrink, you can do this:
UIView.setAnimationsEnabled(false)
collectionView.performBatchUpdates(nil, completion: nil)
UIView.setAnimationsEnabled(true)
collectionView.reloadData()
This forces a reload, and UIKit is able to recalculate the height of the cell given that you update the height. How? In my case I did by updating a height constraint, but there are multiple ways of doing this. However, it's intended that as the reload happens the collection view should be aware of the new cell height.
the good approach for this, you have to add collectionView in the tableViewHeader
then whenever you scroll up your tableView your collectionView section will also scroll up
to load cells on the specific indexes follow bellow steps:
if(indexpath.row == 0){
//load first tableView cell with collection view
}
else if (indexpath.row == 1){
//load second tableView cell with collection view
}
else{
//load table view cell here
}

nested horizontal collectionview in auto size tableview cell exanding height make collection view scroll reset when reload cell for row accured

i have table view for showing product detail and each part is tableview cell,
in one of my tableview cell that is showing sizes and stores that has that size like this
and
when the user click on one of collection view items tableview expand to show stores.
if i update root tableviewcell constraints i need to reload that cell
after reloading collection scroll will rest
i tried beginUpdates and endUpdates for root tableview after changing constraint it will work but in some situation it will show a blank cell
what's wrong in my idea
in summary :
how to expand tableview cell without reloading and refresh cell view .i think both questions are equal
Change the constraints in the tableView and add to model the currently selected collectionViewCell and scroll to it with no animation in collectionViewCell awakeFromNib method the default value is 0 which means no animation will happen [the model I mean here is the model of the tableView from which you display every tableViewCell]

Can we add tableview to cell?

I want to customize my tableview. I want to add another tableview in cell. Can we add tableview in cell of another tableview?
Please give me some ideas.
You can embed a UITableView into another UITableView's cell, but due to potential issues with the embedded UIScrollViews it is not recommended.
If you feel like you need a table in a table, first consider if it is not possible to achieve the desired behavior by using sections and cells in the sections (a section in a table might represent the top tableView's cell, and cells in the given section would represent the embedded tableView's cells).
There is no problem with adding a UITableView into a a UITableViewCell, since both of them are UIViews its simply would be translated to adding a subview to a view...
However, keep in mind to consider the following issues:
1- From a user experience perspective, that would be a vertical scroll view inside another vertical scroll view (are you ok with that?).
2- For each cell -in the main container table view-, you would need to handle the table view delegates/datasources. For understanding the logic of how this could be done, you might want to check:
Is it possible to add UITableView within a UITableViewCell.
Putting a UICollectionView in a UITableViewCell in Swift: although it is about adding a UICollectionView inside the cell, it helps to gain the logic of how to achieve such a task.

Uitableview with UICollectionviewcell and UITableviewcell

i need to create below design
I am following this code to draw simple collection view with horizontal scroll inside a UITableView
Current sample code work like this
UITableView-> UICollectionViewCell as cell with horizontal scroll
What i want
i want : First row with UICollectionview with horizontal scroll and
Then rest row with Normal UITableviewCell to show data
How can i do it .
add a UITableView to your view controller and implement it's delegate and datasource methods .(You can use UITableViewController instead of UIViewController).
for the fist cell of the first section in your tableview, you should use a custom tableview cell.
Inside this custom tableview cell, you have to add your collection view with horizontal scroll, and implement its delegate and data source methods inside your custom tableview cell class.
these are the steps you have to do.
refer this implement uicollectionview inside uitableview question for more.
Hope this will help to you.
You should design a UITableViewCell which has a UICollectionView inside, and set this cell for the cell in section A and for Section B, you should design a new UITableview cell.
When tableview's cellForRowAtIndexPath call you can check for First section and when first section load you can create new UICollection view object load in first cell. Than next section will load you can work as normal tableview work.
Not- you need to implement UICollectionview delegate and datasource methods also.

position custom cell in a tableview not fully showing a cell

Okay What i want to do is position a custom cell so that the cell looks like only a portion of it is shown in the UITableView. When you swipe it to the left it goes to a new viewcontroller that allows the user to add details for a new cell. My question is how do we position the UITableViewCell to show only a portion of it in the tableView?
What i want to do is position a custom cell so that the row is not shown fully like about 1/4th or 1/2 its actual length only in the table view
here is an app with the functionality i'm trying to implement this is the image url as i can't post images yet
http://a3.mzstatic.com/us/r30/Purple6/v4/01/16/ec/0116ec99-0206-d125-7d27-d5956b918635/screen568x568.jpeg
I want to implement my cells just like how they implemented their expenses in cells, the cell is positioned according to the amount in it ,if the amount is high the cell is placed further to the left else only 1/4th of the cell is shown(empty cell no expense )
Can we achieve this by making an imageview in a customcell and then changing the x-axis of the image view according to the amount entered in the cell? Thanks in advance
For this, I would do it this way :
For each of my model cell I add a state, in the delegate of UItableView i use heightForRowAtIndex to set the size depends of the cell's state.
To not show extra view (when the cell is cliped) use :
[myCellview setClipToBounds:YES];
I think it should do the tricks. Even if it's not the best way to do it!
Solved it i Just had to add a scroll view to the cell's content view and then add a view containing the color to the scroll view. So its coding is similar to the swipe deletion in 'UITablView'
https://github.com/TeehanLax/UITableViewCell-Swipe-for-Options
this repository helped me out, i only had to make a few changes otherwise that was it thanks for all your suggestions :)

Resources