How to prevent tableview section head from sticking while scrolling? - ios

I have a tableview with many sections. I am using an actual tableview cell which I dequeue to use as a custom section header. The issue I am having is when I scroll the section header "sticks" to the top of the table until the next section appears.
How can I prevent this from happening and actually have it scroll up like normal?
Here is my code
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HeaderTableViewCell
return cell
}
return UIView()
}

Do following steps.
From #IB
Select your tableView
Go to Attribute Inspector
Find Style Attribute drop down which is Plain by default
Change it to Grouped
Or if you are creating TableView Programmatically use
let tableView = UITableView(frame: YourFrame, style: .Grouped)

Change UITableViewStyle to UITableViewStyleGrouped.

Set UITableViewStyle to UITableViewStyleGrouped, the headers will scroll up with the cells.
and
func tableView(_ tableView: UITableView,
heightForFooterInSection section: Int) -> CGFloat
{
return 0.000001;
}

Step1: Go to Mainstoryboard
Step2: Click on Your Tableview
Step3: Go to Attribute Inspector
Step4: Find Style Attribute drop down which is Plain by default
Step5: Change it to Grouped

Some possible solutions :
If you have just one section then set the Tableview Header property to the UIView you want to set as the Section Header.
If you have multiple sections, change the row count for each section to (number of rows for that section + 1) and make the row at 0th index to be a header. Some hand coding will be required.
Change your tableview to grouped, will require UI redesign so that the design looks similar to a plain tableview cell.

Instead of UIHeaderViewCell, You should use a UITableViewCell. Make section header height as 0 and for every 0th item in that section , display a UITableViewCell with heading on it

Related

Why are separator insets not being respected for the first and last cell in a grouped section?

I want to be able to have all of my separators in a table view to be inset by a certain amount. This table view needs a custom header for each section. See the following image for the desired insets:
My storyboard is set up so that I have a UIViewController which contains a container UIView, which in turn has an embed segue for a UITableView. My table view specifies a prototype cell, and I have set in the storyboard a Grouped style, Default separator, with Automatic inset.
On my Table View Cell in the storyboard, I have a Custom separator inset of 26 on the left.
In my table view controller, I have implemented the following two methods:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ItemHeader") as! TableSectionHeaderViewCell
header.title.text = "My Custom Date Header"
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60.0
}
In a separate file I have subclassed UITableViewHeaderFooterView to just set a font, text color, and some constraints.
Now if I set a custom separator inset on the storyboard at the table view level with 26 inset from the left, either From Cell Edges or From Automatic Insets, nothing changes with the separators that appear below each of my headers. Similarly, if I set these values programmatically in the viewDidLoad method of my table view controller, like so:
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableView.automaticDimension
tableView.register(TableSectionHeaderViewCell.self, forHeaderFooterViewReuseIdentifier: "ItemHeader")
tableView.separatorInset = UIEdgeInsets(top: 0, left: 26, bottom: 0, right: 0)
tableView.separatorStyle = .singleLine
tableView.separatorInsetReference = .fromCellEdges
}
Nothing changes with the separator insets, even using the .fromAutomaticInsets option for the separator inset reference. Here is what I am actually seeing:
It seems that the first and last cells in a section are actually not respecting the separator insets. I thought originally that this was an issue related to having a header for each grouped section, but the separator on the final cell with no header below it seems to indicate otherwise. How can I make it so that these separators all are inset by 26pts? Thank you for any insight you can provide.
Check this thread then, I think it will help:
Grouped UITableview remove outer separator line
I can see that they removed the default separator (Using Clear Color) and put a custom view for the separator themselves.
The other option I can suggest is don’t use a group style Table view, use the plain one and handle it yourself.

Expandable Tableview with last row of every section has Add Button - Swift

I struck in tableview. I need tableview of collapse and Expand looks like Below,
]2
I need to add 2 buttons for last row of every section.
Could you guide me how to design the below design in ios swift?
Add your buttons in custom view and feed it in UITableViewDataSource method below:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
Best way is take Two Button in one cell, and return that cell when indexPath.row = lastRow
And add tap gesture on header view for collapse view
You can create a cell with two buttons. Next you should add it to your data source and configure it properly. Then add a delegate to your custom UITableViewCell and handle taps.

UITableview Height set according to cells

I have UITableView in UIViewController in this view controller total 3 tableview or 2 UICollectionView. so in this one tableview contain many cells or cells data are not static its dynamic according to server. So, I create the table view height outlet & its set on cellforrowatindex function of tableview.
So, we use to set height tableview_height.constant=tableview_height.constant+cell.frame.size.height using this code my problem showing whitespace on the end of UITableViewCell. If I increase the cell then cell are cut from the bottom. I am giving below constraint show in the pic.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if indexPath.row == 0
{
tableview_height.constant = 0
tableview_height.constant=tableview_height.constant+cell.frame.size.height
}
else
{
tableview_height.constant=tableview_height.constant+cell.frame.size.height
}
return cell
}
Check my answer on the almost same question.
How to increase the height of parent view according to height of UITableView in swift?
It is the best way to achieve what you want, because it does not depend on your table settings or if you have header/footer/custom insets/whatever.
Hope it will help.

Renaming header of section of uitableview

How can i rename the title of section header on uitableviewcontroller outside of titleForHeaderInSection ?
So as per the question statement update title without reloading or delegates just using the label reference for updating the title for section.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView() //set the frame accordingly
let label = UILabel() //set the frame accordingly
//make it global and use it reference for updating the title of the section
view.addSubview(label)
return view
}
I am not using Xcode so please validate just writing code roughly
// If only section 2 has a header
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return section == 2 ? updateHeader(section) : nil
}
func updateHeader(section: Int) -> CustomHeader {
let header = (tableView.footerViewForSection(section) ?? tableView.dequeueReusableHeaderFooterViewWithIdentifier("customHeader")) as! CustomHeader
// Set up your header
return header
}
// Change text in header
let header = updateHeader(2).customLabel.text = "Your custom text"
Just a small example. Be aware that when the tableview gets reloaded, it will change back to your default setup of your header. So be sure you handle that within your own code.
You can do it without using titleForHeaderInSection
Make dynamic array 1st object for header custom cell then objects for tableview cell... and so on
Draft custom cell for header
While screen appear use cellForRowAtIndexPath with condition as per header custom cell and objects for table view
At particular event change array value for header custom cell then use reloadRowsAtIndexPaths()
So it will reload only index given in reloadRowsAtIndexPaths(), no need for reloadData().

Remove extra cells/rows whitespace without data appearing on UITableView

I m populating UITableView with data and works fine too.
But my issue is , when the UITableView has less number of rows of data it shows rows with data properly but shows a whitespace .
For example: if the tableview has 1 row to display it shows that row then under that there are whitespaces like empty rows.
I tried
tableView.tableFooterView = UIView()
and
tableView.tableFooterView = UIView(frame: CGRectZero)
Screenshot:
But both just removed the Cell borders not the extra whitespace appearing.
Please suggest. Thanks in advance.
Edit :
Select Button is not Part of the Cell. Just trying to Click on Select Button and then the UITableView Displays under it. ( Like a DropDown ).
Also , using default Tableview Cell not a Custom TableView Cell
Code :
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cityList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cityCellIdentifier, forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row
cell.textLabel?.text = self.cityList[row].cityEn
print("CityName" + self.cityList[row].cityEn )
return cell
}
StoryBoard ScreenShot
If you don't want the empty cells set the TableView style from Plain to Grouped.
Adding to #choli answer You can do 2 things,
adjust the height of the table view based on the rows,
Resize UITableView's height to be as high as it needs to fit only total content size
Simpler solution would be to clear table background color
tableView.tableFooterView = UIView(frame: CGRectZero)
tableView.backgroundColor = UIColor.clearColor()
Sorry, I would comment, but not enough reputation:
This is the tableView size, whenever the full tableview is not filled with rows, it shows some extra rows that are empty. If you don't want this, then you have different possibilities:
you could make the size of the tableView smaller - e.g. the number of cells * cellHeight
you can change the appearance of the tableView, so it will be clearColor instead of white, so the placeholder cells will be invisible.

Resources