`viewForFooterInSection:` not showing custom view - ios

I have this custom view in a UIViewController class:
let footerView: CustomView = {
let footerView = CustomView()
footerView.translatesAutoresizingMaskIntoConstraints = false
footerView.backgroundColor = UIColor.blackColor()
return footerView
}()
Then I have:
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
footerView.title = getString(TITLE)
return footerView
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 44
}
But nothing is displayed. However, if I make that footer view a subview of the viewController's view instead of footer view of the table I have, it is displayed.
What could I be missing? I don't figure it out... Thank you

Related

color for section headers in UITableView

I am creating a table where I want all sections' headers coloured black and sections' footers- green.
Headers/footers are created using the UITableViewHeaderFooterView class
let sectionFooterView: UITableViewHeaderFooterView = {
UITableViewHeaderFooterView()
}()
Code for setting the background colours:
func tableView(_: UITableView, viewForHeaderInSection _: Int) -> UIView? {
let view = mainView.sectionHeaderView
view.tintColor = .black
return view
}
func tableView(_: UITableView, heightForHeaderInSection _: Int) -> CGFloat {
UITableView.automaticDimension
}
func tableView(_: UITableView, viewForFooterInSection _: Int) -> UIView? {
let view = mainView.sectionFooterView
view.tintColor = .green
return view
}
func tableView(_: UITableView, heightForFooterInSection _: Int) -> CGFloat {
UITableView.automaticDimension
}
I get coloured only header/footer somewhere in the middle of the table, but most headers/footers are still clear color. What is there to change?

Unable to add uiview with label in viewForHeaderInSection()

I'm a beginner trying to learn Swift
I'm trying to make my own section header by creating a uiview and adding a label to it before returning it. However the label is never displayed, only the uiview.
I cannot see what I'm doing wrong?
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionHeaderView = UIView()
sectionHeaderView.backgroundColor = UIColor.lightGray
sectionHeaderView.layer.cornerRadius = 0
let sectionLabel = UILabel()
sectionLabel.text = "Test"
sectionLabel.textColor = UIColor.black
sectionLabel.font = UIFont.boldSystemFont(ofSize: 16)
sectionHeaderView.addSubview(sectionLabel)
return sectionHeaderView
}
I forgot to add sectionLabel.sizeToFit()
Thanks maddy!

How do I get the height of a dynamically sized section header in UITableView?

I have a UITableView, it uses a custom UIView as the section header view. Code like so:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ECStartTableHeaderView") as! ECStartTableHeaderView
return headerView
}
I have UITableViewAutomaticDimension for the height on the header. I need to find out what this height is in my code. How can I get this header height?
I think you have to ensure your header layout finished first, so you can get actual height.
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let height = view.frame.height
}
You can take out the Height of the Header View Just Like.
if let headerView = tableView.tableHeaderView {
height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
}

Tableview footer not show

Iam trying to add tableview to view controller. Everything is ok, but footer is hidden/disable? I don't know.
My code:
private let footerView = UIView()
footerView.backgroundColor = UIColor.Green.color
footerView.addSubview(activityIndicator)
tableView.addSubview(footerView)
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = footerView
tableView.sectionFooterHeight = 20
tableView.footerViewForSection(0)
tableView.tableHeaderView = footerView
for sure:
func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "test.."
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return footerView
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20
}
I tried both implementation separate and nothing (white space only).
And my footer view is light gray and implemented activity indicator is "invisible" (frame is set up)
Its ViewController with tableView.
Any idea, why isn't footer view visible right?
Thank you.
You forgot to set the frame of your footerView
Remove all the first part of your code and use this:
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: CGRectMake(0, 0, tableView.bounds.size.width, 20))
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.addSubview(activityIndicator)
return footerView
}
addfooterView()
func addfooterView() {
footerView = UIView(frame: CGRectMake(0, 0, UIScreen
.mainScreen().bounds.width, 150))
footerView.backgroundColor = UIColor.whiteColor()
tableViewLFStation.tableFooterView = footerView }
//call this method when You want to add footer view in your table

Positioning a custom UILabel inside a UITableView footer view

I have a UITableViewController and I am setting the footerView via
self.tableView.tableFooterView = FooterView()
In the class FooterView I have added a label via
self.addSubView(label)
I noticed the following.
The footerView is positioning on top of the tableView
The label is inside the footerview
My questions are
When I set the Frame of the footerView as (0, 0, 100, 100) it seems to be overlapping on the tableView. How do I position the footer at the bottom of the tableView ?
When a label is added via addSubView() of the footer. Is it positioning related to the footerView or the tableView?
FooterView's default height is 44.If you don't set your footerView's height for more and then set your label with height of 100 then it overlaps.
It is positioning with the footerView.
You can set the footerView's height by using UITableViewDelegate method.Below is the way to achieve what you wanted.Footer view only at the bottom of table view.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView .dequeueReusableCellWithIdentifier("cellIdentifier") as! UITableViewCell
cell.textLabel?.text = "trial"
return cell
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if section == 1{
return 100
}
return 0
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2 //The number you needed + 1
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if section == 1{
return FooterView()
}
return nil
}
Delete this line
self.tableView.tableFooterView = FooterView()

Resources