Change Titles of custom Section view in UITableView - ios

I have a UITableView with expand and collapse cells when I tap in the sections. Now I want to collapse the cells again when I tap in one of this cells and give the name of this cell to the section up there. I think that collapse is not a problem but I do not know how to change the name. I have done everything programmatically. Thank you

You can try like this:
add an observer in custom section view to update title. Because section view is reused in UITableView,need a parameter(such as: int sectionIndex) to definite witch section the section view belong to.
in tableView:viewForHeaderInSection: set sectionView.sectionIndex with indexPath.section
in tableView:didSelectRowAtIndexPath: post a notification with indexPath.section and title string

Related

UICollectionView: how to scroll to supplementary element instead of cell?

So here is my setup:
UICollectionView configured with UICollectionLayoutListConfiguration
appearance is UICollectionLayoutListAppearancePlain
headerMode is UICollectionLayoutListHeaderModeSupplementary
Using UICollectionViewListCell with content configuration
Implementing collectionView:viewForSupplementaryElementOfKind:atIndexPath: to return custom section header
Implementing indexTitlesForCollectionView: to return index titles representing the sections (e.g. "A", "B", "C")
Implementing collectionView:indexPathForIndexTitle:atIndex: to return the index path of the first cell in given section.
Now, when user taps on any index title, the collection view scrolls and positions the first cell of that section at the top. However, the header section (which is sticky) is now overlapping part of the cell.
Any suggestion on how to fix this?
I guess that a new delegate method, such as collectionView:supplementaryElementIndexPathForIndexTitle:atIndex: would probably target this use case (as the collection view would scroll to a section header rather than to a cell).

How to add a subview to a TableViewCell?

I need to add a subview to TableViewCell when the button in TableViewCell is clicked it should show another tableview as a subview and height of the tableview should be dynamic according to number of cell.
How can I do this?
Above screenshot is taken from a very popular shopping app and I need to do the same in my project.
You can easily achieve this using simple UITableView. For that you need to set all your main categories as your UITableview section and respected sub-categories can be added to respective rowOfSection.
You can use just one table view with UITableViewStyleGrouped style, and set "Men","Women","Kids & Baby" as TableView sections header, keep an boolean value to determine the result of "numberOfRowsInSection:" of each section and reload tableview.
See the below link at github:
https://github.com/OliverLetterer/SLExpandableTableView
This contains the Expandable TableView, which you required. You have to implement SLExpandableTableViewDelegate and SLExpandableTableViewDatasource which contains different method, in which you have to provide inner tableview as well.
Hope this helps you.
You need to do some work on your own I can give the directions that'll give you a way from my point of view :-
Make a Custom cell which you want to expand.
While designing the cell make its height in storyboard like 200 or so according to your need and add all the elements those you want to see when the cell is expanded.
You'll need two delegate methods first -didSelectItemAtIndexPath and second HeightForRowAtIndexPath at index path.
First You need to make sure the user taps on the button or cell that you want to expand , and to achieve that you need to call didSelectItemAtIndexPath.
Once you get your cell Position, in HeightForRowAtIndexPath check the indexpath is equal to your cell's indexpath,if yes then return the exact height(ie:200) of your cell otherwise return the default(ie:70) height of you cell.
Note : In didSelectItemAtIndexPath you need to call to method to update the current cell
[self.tableView beginUpdates];
[self.tableView endUpdates];

How can i refresh buttons inside a cell in CollectionView?

Given that:
For example in a collectionView there are 8 cells & each cell has a button & an image.Buttons here will display Yes/No Option to the user. Out of 8 cells only 1 cell will show "Yes" and remaining cells should show "No".
What i am looking is?
When i tap on a different button then current button show display "Yes" and previous button display "No".
I don't want to reload entire collectionView or cells inside a section in collectionView.
You can make use of reloadItemsAtIndexPaths method of collection View to reload multiple cells(In your case 2 cell i.e the minimum number of cells to be reloaded) instead of reloading whole collectionView.Pass the indexpath of the cell that you want to reload
ex:
//Logic to change button title Yes to No comes here.
collectionView.reloadItemsAtIndexPaths([indexPath1, indexPath2])
indexPath1, indexPath2 are the index paths to be reloaded.

header cell on top of list

I have a tableview with a list. I want to add a cell to the top of the list that shows what is being displayed below. So each item in the list is a Name with Age and Gender next to it. I want a header cell on top that actually says "Name Age Gender" so that people actually know what data is being displayed. I also want this cell to not scroll when the rest of the list scrolls downwards.
Do I add another cell to show these things or should I do it via another method?
I think you're talking about a header of a table view section. If so, you can implement the header cell methods in UITableViewDataSource:
-tableView:titleForHeaderInSection:
Alternatively, if you want to get fancy and use your own view for a section header try implementing the UITableViewDelegate method:
-tableView:viewForHeaderInSection:

How to distinguish between UITableView custom section click and section first row click?

UITableView returns the same callback "didSelectRowAtIndexPath" with the same NSIndexPath (0,0) both for section click and section first row click. I'm using custom view for section header view and I need to perform some action on these section rows. Tried checking cell class with [tableView cellForRowAtIndexPath:indexPath] but it's obviously returning the same row cell instead of section cell. Any suggestions?
UPDATE I could add my custom section view to first row instead of adding it as a section, however in that case, I would need to return different row height in "heightForRowAtIndexPath" and that would be not-performanc-wise decision.
UPDATE I'v designed my section view as a subclass of UITableViewCell, because I prefer to get native UITableView callbacks instead of workaround'ing with tap gestures or buttons.
Centurion, if you want to have section that you open/close easily, I suggest you to use the class APLSectionHeaderView.
You can find more information on APLSectionHeaderView.h and APLSectionHeaderView.m
Hope it will help you.
I've been using it, so if you have some question about it...

Resources