UITableView inside UITableViewCell - didSelectRowAtIndexPath - ios

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.

Related

I need help knowing where to start on designing a tableview or if a tableview is what I need

As in title, I don't know where to start with creating a view like this. Do I hard code it or use XCode editor? Is a table view what I need?
Yes, a UITableView is what you needed.
You need to create a TableView Controller.
Follow by creating a TableViewCell
Then you need to start coding. Hook up the delegate and data source methods.
For more information, check this tutorial by Apple:
https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/
Here you have a tableView with section header indicating the date of the matches, the tableView has a custom designed cell, each cell contains the names of the two competing team.
start by adding the tableView to your viewController using storyboards, with the delegate and datasource connected.
the sub class from UITableViewCell and create your custom cell with an xib file.

CollectionView nested inside Collectionview

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/

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

Swift custom cells layout TableView decision

I need to display a table with in my iPhone app:
neither the number of cells nor the contents are known at compile time, but only at run time.
Views for each cell may differ, one cell has textField and another may have some other view control.
Should I consider Static or prototype cells?
Should I consider tableViewController or viewController with tableview in it?
Any thing I need to consider before I start coding? Thanks in advance
For The issue of dynamic Number of cell at Run time, you can call reload data for table view at any time you have the data source ready.
Prototype Cells should be used with no problem.
Simple Table View will be sufficient for the task.
You have to make cell, either in code or in storyboard, for each type of cell you want, 1 table View can have multiple types of prototype cells, Just name them differently and then make the objects of only the specific cell of which the data is best suited.
It is not that difficult but do handle the data source with extreme care.
Should I consider Static or prototype cells?
If you know all possible subview combinations that your cells might need to display the data appropriately, and they are relatively few, make one prototype for each. for example:
One text field,
Two labels,
One image view and a label,
...etc.
Otherwise, just use the plain-vanilla UITableViewCell (no subclassing) and remove/add subviews at runtime when reusing them (that is, inside the method -tableView:cellForRowAtIndexPath:).
Should I consider tableViewController or viewController with tableview
in it?
The only reason I would choose UIViewController + UITableView over UITableViewController is if -for example- I needed the table view to only take up part of the main view's bounds/screen, and display some other subview in the remainder. Otherwise, you get so much "for free" with UITableViewController that there's just no point in implementing all of that from scratch.
You have to choose prototype cell, u can create different types of cell depending upon your requirement.Any think ok for u, u can create tableview controller or view controller.

UICollectionView: Is there a way to reload only the supplementary views?

I have an uicollectionview with supplementaryView for each section.
In each supplementaryView I have some buttons.
In dataSource method collectionView:viewForSupplementaryElementOfKind:atIndexPath: I'm setting the buttons tags with the indexPath.section. I made some delegate methods when a button is pressed and send the section(button.tag) as parameter.
Everything works fine, but here comes the problem:
when I insert or delete a section it must update the buttons tags, but I don't want to reload all the items from each section.
I've tried:
to get the supplementaryViews with the dataSource method collectionView:viewForSupplementaryElementOfKind:atIndexPath: and call layoutIfNeeded method for each supplementaryView, in this way the supplementaryViews are reloading, but this approach multiply my supplementaryViews.
a fast for in collectionView.subviews and get just the supplementaryView and call layoutIfNeeded method, but it doesn't reload, just the layout is reloading.
reloadSections: method, but this reload all items from the section
Ohh.. And I use NSFetchedResultsController(delegate) for inserting and deleting sections.
Can anyone help me with this? Is there a way to reload only the supplementaryView? Is there other approach for this scenario?
Thank You!
To reload supplementary views you can use -invalidateLayoutWithContext:.
See documentation for UICollectionViewLayoutInvalidationContext and method -invalidateSupplementaryElementsOfKind:atIndexPaths: (available since iOS 8).
Instead of reloading these views, getting a reference to each visible supplementary view so that you can change the tag might be sufficient for your needs.
There is a simple method you can use to do this:
let supplementaryViews = collectionView.visibleSupplementaryViews(ofKind: "identifier")
https://developer.apple.com/documentation/uikit/uicollectionview/1618026-visiblesupplementaryviews

Resources