Section within a section for UITableView - ios

I have to create a UITableView that has sections within sections however I have never done this before and am not even sure if this is possible.
This is what it needs to look like
Header 1
Sub Header 1
cell 1
cell 2
cell 3
Sub Header 2
cell 1
cell 2
Header 2
Sub Header 1
cell 1
Sub Header 2
cell 1
cell 2
All fields are dynamic, so there could be 0 or more Header sections; would a UITableView be the best way to go about this? if so how would I approach this?

There is no easy way to do it, you have to plan cleverly.
From numberOfSectionsInTableView return the number of "Header".
For each section in numberOfRowsInSection return Sub Header + Cell in each sub header.
From cellForRowAtIndexPath just chek if it is a sub header or a cell. If its is a sub header the return a cell which has a Label near to left side, if it is a cell then return a cell that has the label in more far away from left border.
You can use same cell just changing the frame of the cell's label.

Related

Swift Dynamically growing a cell depending on the text length

I have a TableViewController which contains 2 sections:
Section 1 - is a cell which is loaded from a xib file. This cell just contains a TextView.
Section 2 - Contains multiple cells populated from an Array.
The section 1 only exists if the master (previous) UITableView cell you select contains a certain piece of data.
All of the above works as expected, below is the parent view. The list of items come from a database, some items have a description, and some do not. For example below this image, you'll see the view is 'Classic Starters' is selected. Then below that, you'll see the view if 'Stir Frys' is selected. Stri Frys contains a description:
Now, what I want is, the description cell which is shows on the Stir Frys page, to automatically grow depending on the length of the text inside it. So if a description is 10 lines long, it will grow to show all 10 lines.
Does this have to be done programmatically, or is their a feature in XCode I'm missing ?
You can use UITableViewAutomaticDimension.
1)
Set properties estimatedRowHeight and rowHeight of your tableview, in viewDidLoad for example. Like this :
self.tableView.estimatedRowHeight = 50
self.tableView.rowHeight = UITableViewAutomaticDimension
2) Return UITableViewAutomaticDimension in your heightForRowAt delegate method :
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
First you need to do is to set estimatedRowSize to a value that best estimates most common size, and rowSize to UITableViewAutomaticDimension:
self.tableView.estimatedRowHeight = 44
self.tableView.rowHeight = UITableViewAutomaticDimension
In your case, since except the first one all the other cells are supposed to be the same, you can use the height of the rest of the cells as the estimatedRowHeight.
You don't have to implement heightForRowAt at all.
The second step you need to do is to setup proper constraints on the cells. That means you have to constrain the contents of the cell to the left, right, top and bottom of the cell, so that when contents grow, the cell will need to grow, too. Common mistake is to forget to constrain bottom, so then the cell does not grow and the contents leak through the bottom of the cell.
Third, since your dynamic cell contains UITextView, you need to make sure that it will grow with its text. That is not automatic. To achieve that, based on this answer, this should suffice (in the cell):
textView.isScrollEnabled = false
If you are using storyboard, just uncheck scroll enabled.

TableViews inside ScrollView Autolayout

I am trying to create a View that contains many subviews and can contain many tableviews.
Need is to enable scroll of all the subviews.
Actually I created scroll view and put it into superView. Then i put all the views inside that scroll but i have a lot of problems with putting tableviews inside that scroll using AutoLayout. I dont want a scroll inside tableViews, all i need is to enable clicks each tableView i create. I would like to listen to all of your suggestions how can I easily create this View.
A scrollview inside of a scrollview is hard to handle. Maybe it's worth to redesign your data model by combine the contents?
e.g. (assuming that each table contains 2 Labels)
image -> Row cell 0 of type A OR TableHeader
Label -> Row cell 1 of type B
Label -> Row cell 2 of type B
Label -> Row cell 3 of type B
Label -> Row cell 4 of type B
Table 1 Label 1 -> Row cell 5 of type C
Table 1 Label 2 -> Row cell 6 of type C
Label -> Row cell 7 of type B
Table 2 Label 1 -> Row cell 8 of type C
Table 2 Label 2 -> Row cell 9 of type C
Label -> Row cell 10 of type B
Table 3 Label 1 -> Row cell 11 of type C
Table 3 Label 2 -> Row cell 12 of type C
...
Button -> Row cell n of type D OR TableFooter
Now you can fill your table like a normal table without special solution.

How to I build a continuous parallax scroll/Collection view?

I am looking to build a continuous parallax scroll like this.
http://www.screencast.com/t/7Z48zkkW
But my Requirement is view and then CollectionView like this
When collection view scroll then changes upper view height.
and yes upper view and collection view is separate.
I Have also navigation bar it also goes up when user scroll
You can use collectionView with section header for this effect. However, the effect collectionView bounced below the search can't be achieved by this way instead the whole view will bounce.
Simplest solution for iOS 9 +
1) Make a cell for items above the search
2) Make reusableView for search bar (section header)
3) Make cells for those pictures
i.e. you will have two cell's prototypes and one reusableView
In viewDidLoad of viewController with collectionView use following code so that header of section gets pinned always in top:
let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout // casting is required because UICollectionViewLayout doesn't offer header pin. Its feature of UICollectionViewFlowLayout
layout?.sectionHeadersPinToVisibleBounds = true
Number of section: Keep it 2
Number of row for section == 0 will be 1 which will be items above search
Number of row for section == 2 will be number of images you have
Now, in collection view datasource you have a method viewForSupplementaryElementOfKind which you need to implement to get sectionHeaderView.
implement referenceSizeForHeaderInSection function for hiding sectionHeader for first section (section == 0) by returning CGSize(width: 0, height: 0) and for second section (section == 1) return size of searchBar as you want.
in cellForItemAtIndexPath return cells according to section.
EDIT
I think you don't need sticky header at top like that search bar. So its better you use collectionView with section header. (section header contains the red View you marked)

Conditionally hiding cell separators and removing hidden section space Swift

I want cell separators in every single one of my sections other than section 0.
Between "New Game", "Friends", "Random", "Judge", and "Challenges" I don't want any separators. The cell type in section "0" or "New Game" is different than the other sections' cell types so I tried this: iOS swift remove UITableView Cell separator space but it didn't work.
Another issue I'm facing is how to erase the space left when a header is gone because it has no rows in it.
Here is a picture:
As you can see, "Waiting For Opponent" has disappeared because there are no rows in the section.
So to be extremely clear:
How do I conditionally hide the separators in section 0 without hiding the entire tableView's separators?
How do I hide the space that is created when I hide a section header?
I can think of two options:
Hide the separators entirely and implement your own separator by adding a UIView with 1-2pt height placed at the bottom of the cell which can then be hidden/unhidden based on your requirements in cellForRowAtIndexPath. OR
In cellForRowAtIndexPath, change the insets of the separator for that cell so that the separator goes out of the view. Eg:
cell.separatorInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, cell.bounds.size.width-cell.layoutMargins.left)
Have you tried returning zero in tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat ??

scrollup uitablviewcell partially visible

I need some help.
That i want to scrollup uitalbeview cell which is partially
displaying.See screen shot.
Here my tableview has a three section and u can see that in morning
section the row is displaying partially.I just want to move up that
kind of row.
I tried content offset but not getting any idea that how to get first
visible row offset so,i can calculate and move it up or down
.
Note: not for bottom row only top rows.
This is the default behavior of a UITableView. The section headers will stick at the top of the screen until the next section header will reach it. One easy work around is to not use headers at all and simply use two cells. You would then have to add code in your viewForCellAtIndexPath:
if(indexPath.row == 0) {
//setup a header cell
} else {
//setup a data cell
}

Resources