how to expand and collapse multiple row and sub row in tableview - ios

I have a table view. Now I want to collapse and expand rows by tapping on the section header. In other words, when I tap the header the rows display for that section and taping sub row its display other section How can I do this?

You can return your own header view by implementing tableView:viewForHeaderInSection: method (of table view data source). Here you can add tap gesture on header view. Hence when header view is tapped, you can maintain, which header view is tapped in some data structure and should it be expanded or not. Then you can call tableView reloadData method to reload the data and in tableView:numberOfRowsInSection:& tableView:cellForRowAtIndexPath: methods, you can check if this header view is to be expanded, you will prepare the cell for rows for this header view else you will not.

Related

Using the same table view cell in multiple view controllers

I have a custom table view cell class, which I use in several view controller's table views. I am adding a feature that gives the user the ability to delete a table view cell and I'd like to remove the cell from the table view when they do so.
Using a delegate is one way to achieve this, so that when the user deletes the table view cell, the table view cell class informs the view controller, through the delegate, that it was deleted and to remove it from the table view. However, this would mean I'd have to do this for each of my several view controllers, which I don't want to do.
So my question is, how can I implement this in such a way that I only have to write the code to remove the table view cell from the table view once?
Subclass the UITableView. Create a protocol like:
protocol customTableViewDelegate {
func deleteTableViewCell()
}
Then give the subclassed UITableView a variable of this type:
var removeCellDelegate: customTableViewDelegate?
And then instead of adding a normal UITableView to your view controller, add the custom one and set the removeCellDelegate to the view controller. Then implement the function to do whatever you want.

iOS Storyboard - TableView Embedded in Container with Dynamic Height

I have a tableview that is embedded in a container, its using prototype cells so in theory I don't know how many cells there may be (realistically 1 - 3). At the minute, I end up with a bunch of empty rows underneath the table cell that is populated. Changing the height of the container can sort of 'hide' these but it isn't the solution.
How can I get the containers height to change depending on the number of items in the embedded tableview? Or alternatively if anyone knows if there is some magic "don't add any empty rows if there are only 1 or 2 items in my table view" setting?
You will have to set the frame of the Container with the embedded TableView based on the contentSize of the TableView. Note: depending on how you setup the TableView data sourse, the contentSize property of the TableView might not be set in viewWillAppear. Instead, you will have to use the viewDidAppear method of the ViewController that has all of your Container views. Also, you might need to call reloadData on your TableView after you set the frame of the Contatiner view.
Another option is to use the -prepareForSegue: method to make your parent view controller a delegate of the table view controller. When cells are added/removed from the table view, it can call a method on the parent view controller and pass the info; the parent view controller can then change the size of the container based on that information.
https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

What does clearsSelectionOnViewWillAppear do?

clearsSelectionOnViewWillAppear is a boolean property of table and collection views, and it's set to true by default. According to the Apple Reference:
A Boolean value indicating if the controller clears the selection when the table appears.
What does that mean?
Let's say that, as is usual, you tap a cell and this presents a new view controller. Okay, but how does it do that? Your tap selects the cell. That is a visible change in the cell - the cell is highlighted.
Later, you dismiss that view controller and return to your table / collection view.
The question is: should the selection (caused by your earlier tap) still be showing as a selection? Should the cell still be visibly highlighted? The default is: no. We clear the selection before you visibly return to the table / collection view.
(You could do this yourself in your viewWillAppear: implementation, but the table / collection view controller is willing to do it for you.)

Notification when table view row is deselected during multiselection IOS

I have a table view with multiselection enabled. I want to call a webservice when the table row is selected and a different webservice when it is deselected. I have noticed that on table view selection the didSelectRowAtindexPath is called but its not called on deselection. Is there any way to trigger an event when a row in table view is deselected.
See the UITableViewDelegate methods tableView:didDeselectRowAtIndexPath: and tableView:willDeselectRowAtIndexPath:.

UITableView header view

I have 3 different instances of tableview in Nib, where I am displaying all these tableview in a page control. And delegate and data source for all the 3 tableview is same.
Problem here is, Header view is appearing in one tableview and disappearing in others when I change the Page of the UIPageControl ?
Any idea how can I resolve this issue ?
Since it's not possible for a view to be a subview of more than one view at a time, using the same view as a header for multiple table views will result in odd visual artifacts (views disappearing one place and showing up somewhere else). You can add one header view per table view to your nib, or generate a header view for each programmatically, and assign them at run-time.

Resources