How to Expand/Collapse Collection view section in Swift? - ios

How to expand/collapse Collection view section by taping on the header of collection view in Swift?

I guess you want to show section only, and expand/collapse cells.
Then You can treat your section as a particular cell, once you want to expand/collapse, add/remove data and then call "reloadRowsAtIndexpaths" method.

Late to the party, but I guess there is some 3rd party work done on this over here
The basic idea is to roll out your own UICollectionViewFlowLayout subclass and proceed from there on. But as you can see in the repo, the devil is in the details.

Related

Swipe to delete tableview section [duplicate]

I am working on extended data and a UITableView. I am able to use swipe to edit and delete functions. I am wondering if it is possible to swipe and delete only section headers. If yes, how?
I am not sure what you want to achieve.
But if you want to have a swipe delete for a section header, the answer is, you can do it, but it requires some kind of programming. Essentially, you had to add a „section header delete button“ as a subview to the table views content view.
I suggest reading Ray Wenderlichs tutorial „How To Make A Swipeable Table View Cell With Actions – Without Going Nuts With Scroll Views“. This demonstrates how you can add swipe gestures to a table view cell.
However the same techniques (e.g. controlling the position within the scroll view) can be applied to a section views header.

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

Swipe to Delete Section Header in UITableView

I am working on extended data and a UITableView. I am able to use swipe to edit and delete functions. I am wondering if it is possible to swipe and delete only section headers. If yes, how?
I am not sure what you want to achieve.
But if you want to have a swipe delete for a section header, the answer is, you can do it, but it requires some kind of programming. Essentially, you had to add a „section header delete button“ as a subview to the table views content view.
I suggest reading Ray Wenderlichs tutorial „How To Make A Swipeable Table View Cell With Actions – Without Going Nuts With Scroll Views“. This demonstrates how you can add swipe gestures to a table view cell.
However the same techniques (e.g. controlling the position within the scroll view) can be applied to a section views header.

expandable cells table view best practice for ios

For the iOS we are not been provided any expandable list view as a component. so to use the expandable list we are having 2 basic options:
Option-1: Table header as the main view and then clicking on it open cells under that header.
Option-2: Cell as the main view and add more cell below as if its sub cells when clicking on any of the cell.
And more than this 2 different developer uses different logic. so Can we know that which would be the best practice to use expandable/ collapsable Table view in iPhone/iPad application.
Thanks in advance. This could be consider as a knowledge sharing or better coding practice related question.
There are a million ways you could do this.
In the past I have created a custom "stretching" cell that would stretch its own size to show items that were hidden. This could be a way of doing it.
What you would need to do is have a cell that when clicked, opens up (revealing a new cell hidden underneath it). You could also have it seem as if there is another cell being shown below the clicked index, but in reality you are only adding a new cell at cell_index_clicked + 1.
Hope this helps you think it out.
Use iOS 8 auto layout ..........

To use Flow Layout, or to Customize?

I'd like to build a collection view layout as outlined in the diagram below.
Primarily, I'd like to keep all items for a particular section on the same line. When the users scrolls vertically, the sections scroll off the page. When the user scrolls horizontally, the items left and right ("tucking" under the section header when scrolled left, bringing new items on screen from the right).
I understand Apple says "If it looks like a grid, you can use Flow Layout," but I'd like to understand if that's just a blanket statement, or if the above will push the limits.
Can layout above be done using the FlowLayout, or am I better off building a custom layout from scratch?
If you're a guru with layouts and this is "child's play," I'd love to see an example of how to proceed.
Thanks!
You need to use layout class which will inherit UICollectionViewFlowLayout. By doing this, you can customise your behaviour of layout by using below methods. Also you can get all hooks of Flow Layout also.
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect; // return an array layout attributes instances for all the views in the given rect
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)prepareLayout;
Rather than going to the trouble of creating a custom UICollectionViewLayout subclass, you can achieve the behavior you're looking for by using a standard table view, with a cell for each row that contains one view for the header, and a horizontally-scrolling collection view for the row items.
I put together a little example that you can download here.
A little extra work is needed to maintain the scroll position of the collection view as you scroll vertically through the table view, so I'm saving that into an instance variable, and restoring it when the table cell is requested. Other than that I think the example project is fairly straightforward.
Here is my implementataion! Hope this will help you!
You cannot subclass UICollectionViewFlowLayout here because it is a single-line based layout.
Thanks for the excellent answer. It works for me. You can also specify one single line per tableview section and add header section for each collectionview.

Resources