Cannot disable footer for section in UITableView Grouped - ios

I am using UITableView grouped and using multiple sections. I am using these codes to hide the footer for sections.
self.tableView.estimatedSectionFooterHeight = 0
self.tableView.sectionFooterHeight = 0
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.0
}
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
Only Footer for section 0 is not getting hidden.
Here is the screenshot:
Please help!

If you are using grouped tableview then there will be footer always and it never accepts 0 value. But you can return 0.5 or 1 to fix your problem and no need to override viewForFooterInSection

Sorry guys to bother, what I have done is I was also hiding sections and header which don't have any rows.
So one of the header was not getting hidden as I was returning empty UIView() in viewForHeaderInSection. Though I was returning 0 in heightForHeaderInSection for sections which don't have rows.
The problem is now solved as I am returning nil in viewForHeaderInSection.
Screenshot:

Related

How to display title for header text for header in first section iOS

Using method below of viewForHeaderInSection i try and display an image in table header section one , but i also want a heading text, using the method of heading title text i am able to display the heading for all sections but not section 1 , do i need to make some changes to heading view for section one to be able to display the heading , if i were to remove the viewForHeaderInSection then the title for first header section also shows up
let sectionTitle = ["Feedback", "Follow Us"]
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
headerTop.headerLogo.image = UIImage(named: "foodpin-logo")
return headerTop.topView
}
return nil
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitle[section]
}
You need to create a text label in your headerTop view and remove this function override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int). So your code will be like this:
let sectionTitle = ["Feedback", "Follow Us"]
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
headerTop.headerLogo.image = (section == 0) ? UIImage(named: "foodpin-logo") : nil
headerTop.yourHeaderTextLabel.text = sectionTitle[section]
return headerTop.topView
}
It seems like you are looking for tableHeaderView! The issue is you are overriding the first section's header.
You may consider setting the tableView's tableHeaderView for the headerTop instead. So all sections will have their titles as you expected.
tableView. tableHeaderView = headerTop
Also, if you want yout table's header to be sticky, there are bunch of tutorials for that.

Margin before first Table View Section

Why do I have margin before first table view section?
Is it not supposed to be only between sections?
Overriding
tableView (_tableView: UITableView, heightForHeaderInSection section: Int)
You will get the behavior you want at runtime. If you need to change the values according to the section, you need to do it manually by testing which section you are in.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
if section == 0 {
return CGFloat.leastNormalMagnitude
}else{
return 44
}
}

Table View Height Dynamically

I implemented expandable sections on my UITableView and now my problem is that when the UIViewController loads the table there are rows below the section.
Not expanded:
Expanded:
I want the UITableView to have no rows if the section is not expanded so I can set another UIView below the section. When the section is expanded the UIView should disappear and the table view to be presented on the whole screen.
Simply implement the viewForFooterInSection and return an empty view.
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: .zero)
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
just use this code it will remove your unwanted rows.

heightForHeaderInSection only called once

Coding in Swift 3. Have a tableView with custom cells and header.
I have a tableView with custom cells and headers. The headers have two (2) labels in them and have dynamic cell heights since the labels may be long. My problem is the first time the tableView and sections are configured the label appears as it should, HOWEVER, after scrolling down and then back up the headers' layout somehow breaks.
As you can see below, after I scroll down then back up to the cells, the label is getting cutoff.
After printing out what methods are being called I found that the first time scrolling down the tableView the following two (2) override functions are called.
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
print("\(section) heightForHeaderInSection")
print("\(section) returning AUTO for header")
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print("\(section) viewForHeaderInSection")
let header = tableView.dequeueReusableCell(withIdentifier: "QuestionHeader") as! QuestionHeader
header.delegate = self
header.contentView.backgroundColor = UIColor.groupTableViewBackground
header.questionTextLabel.text = String(questionStringArray[section])
header.questionNumberLabel.text = (String(section + 1) + ")")
return header.contentView
}
But when i scroll back up ONLY the viewForHeader function is called and I think because the height is no longer being set to UITableViewAutomaticDimension the labels get cutoff?
Any ideas?
You should return header instead of header.contentView from tableView: viewForHeaderInSection: method:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableCell(...
...
return header
}

UITableView headerView different sizes

I'm trying to apply a size on my UITableView headerView but it seems like the first section header always is a bit smaller than the rest section headerViews. I've said the constraints for the UITableView so it should be fine. How come the first section is smaller?
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return ""
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
This behavior occurs when using a Grouped UITableView. The section header height appears shorter for the first section header, because it doesn't have a footer above of it. However, every section header after the first one, does have a footer directly above it. The footers have a default height, which is contributing to the overall height appearance of the section header. Therefore, set the section footer heights so the space between the group sections will have the same visual height.
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.min
}
We use CGFloat.min instead of 0, because returning 0 will cause the footer to return a default height.

Resources