How to add a subview to a TableViewCell? - ios

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];

Related

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 within UITableViewCell does not get updated

I have tried to create a custom cell that would display a list of items. To achieve this I tried to create a custom cell with UITableView inside it with scroll disabled.
The problem I have with this is when i try to apply changes to the cells in the inner UITableView data just does not get updated and the cell stays as it was. I have tried calling [tableView reloadData], [cell setNeedsDispaly], [cell setNeedsLayout] to no avail.
It seems like the data that has been applied when the cell was initialised persists through any attempts to change it. Though, when I create a breakpoint in cellForRowAtIndexPath: the data does get updated but is not rendered.(e.g. text property of UILabel has new value, but text is old on the screen.)
You need to nil the cell and then reload the table view. For some reason iOS caches table view data and stuff doesn't get updated correctly.
You could try overriding UITableViewCell's prepareForReuse(): in that method you can set all the cell's outlets/properties to nil. You would do this for the cells in the main cell's table view. I am assuming those are custom cells as well and that you have a custom UITableViewCell subclass for them.
Refer [I have two views on one cell, when I click on a cell it will be hidden and one edit form will be expanded on that. How to resolve that? ..check in accepted answer methods ...
[tableView reloadRowsAtIndexPaths:ll withRowAnimation:UITableViewRowAnimationAutomatic] in didSelectRowAtIndexPath

UITableViewCell next to eachother

How can i make a tableview where the cells are next to each other. Where there are 2 on each row. example like this? In the second row there are 2 images next to each other. Can this be done in a tableview by making custom tablevieCells?
Yes this can be done in UItableViewCell But preferred to use collection view for this kind of view.
Just subclass uitableviewcell, add new method
-(void)setCellWithNumberOfImages:(NSInteger)images withImage1:(UIimage *)image1 withImage2:(UIImage *)image2;
if(image2 ==nil)
//add only one UIimageView with image1
else
//add two imageviews.
You need to use a UICollectionViewController (https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewController_clas/Reference/Reference.html)
Using a UICollectionView you can implement a custom layout which layouts the elements as in your screenshot
You can Use UICOllectionView That's much easier and customizable, you can chose how many item you want to have in a row and size for example.
for more information please check: UICollectionView Class Reference

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...

How to make two tableview cell in one controller class in iphone

I am making two tableview cell in one controller class on vertical style. I have different value both the cell. Left cell have some name value for eg(name :pradeep),and right side cell have some price value so if select on left cell on 3row and right side cell on 2 row so this two cell value I have to pass for open url.
Please help on this how to know which cell index are selected from both the tableview cell and what the value on that selected cell.
If I understand your requirements correctly then you should probably be using a UIPickerView:
http://www.youtube.com/watch?v=wnxS35JDqok
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerView_Class/Reference/UIPickerView.html
You can add multiple tableview cell. You need to separate or identify them using Identifier. After creating custom cell, you can add tableview cell into Cellforrowatindex path method.Make sure don't forgot to import "custom cell.h" in your implementation file. Thanks.

Resources