Tableview footer not show - ios

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

Related

How can I access to the FooterView of a UITableViewController with .grouped style

I'm trying to do a simple task, add a text to the footerViews for every section in my grouped Table View. I'm working for first time without Storyboards and maybe I'm missing something 😩
I should mention that doing the same with a .plain style is working.
So I'm creating my UITableViewController subclass instance:
let settingsController = SettingsController(style: .grouped)
Then I'm implementing all the possible methods in reference to footerView:
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 50))
}
override func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.font = UIFont(name: "Futura", size: 11)
header.textLabel?.textColor = UIColor.lightGray
header.textLabel?.text = "TEST"
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40
}
Why is entering in these methods when is setting up with .plain style but not with .grouped, any idea? Thanks 👍

Set a custom table view footer with Swift

I am trying to set a custom footer to my table view. I created the footer on a nib file and created a controlled for it.
class LoginTableFooter: UITableViewHeaderFooterView
In viewDidLoad() I wrote this code
let footerNib = UINib(nibName: "LoginTableFooter", bundle: nil)
tableView.register(footerNib, forHeaderFooterViewReuseIdentifier: "LoginTableFooter")
Then I implemented viewForFooterInSection
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let cell = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "LoginTableFooter")
let header = cell as! LoginTableFooter
return cell
}
viewForFooterInSection was never called. I also tried to implement viewForHeaderInSection but it was also not called. Do you have an idea what is wrong? I have only one section in my table view; is it possible/better to set the footer directly on viewDidLoad?
Implement - delegate & datasource for your tableview and set both - heightForFooterInSection & viewForFooterInSection
tableView.delegate = self
tableView.dataSource = self
// set view for footer
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
footerView.backgroundColor = UIColor.blue
return footerView
}
// set height for footer
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40
}
//firstly you need to call the delegate and datasource of table view. i.e:
tableView.delegate = self
tableView.dataSource = self
//you need to call this delegate
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return "your expected footer height"
}
Swift 3
Directly use > viewForFooterInSection
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
footerView.backgroundColor = UIColor.red
return footerView
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
footerView.backgroundColor = UIColor.red
return footerView
}

`viewForFooterInSection:` not showing custom view

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

Change the sections header background color in UITableView using an array of headers

I have a array of headers that I use
let sectionHeaderTitleArray = ["test1","test2","test3]
and they are showed using
func tableView[tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderTitleArray[section] as String
}
Now all of this works fine but I would like to modify the background color of the headers so that they are more visible (Darker Color)
any idea if I can do this in a simple line or do I need to use a custom cell to create this
thanks
update
//number of sections and names for the table
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sectionHeaderTitleArray.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderTitleArray[section] as String
}
func tableView(tableView: UITableView, ViewForHeaderInSection section: Int) -> UIView? {
return self.tableView.backgroundColor = UIColor.lightGrayColor()
}
If you're using only titleForHeaderInSection :
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
(view as! UITableViewHeaderFooterView).contentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
(view as! UITableViewHeaderFooterView).textLabel?.textColor = UIColor.white
}
Instead of using the
func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String?
data source method, you can use the
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
delegate method and simply customize the UIView returned as you wish.
For example set the text of the UILabel textLabel to your desired value and the backgroundColor to the desired UIColor.
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let returnedView = UIView(frame: CGRectMake(x, y, width, height)) //set these values as necessary
returnedView.backgroundColor = UIColor.lightGrayColor()
let label = UILabel(frame: CGRectMake(labelX, labelY, labelWidth, labelHeight))
label.text = self.sectionHeaderTitleArray[section]
returnedView.addSubview(label)
return returnedView
}
SWIFT 5
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let returnedView = UIView(frame: CGRect(x: x, y: y, width: width, height: height)) //set these values as necessary
returnedView.backgroundColor = .white
let label = UILabel(frame: CGRect(x: x, y: y, width: width, height: height))
label.text = self.sectionHeaderTitleArray[section]
returnedView.addSubview(label)
return returnedView
}
You have to keep both
titleForHeaderInSection
AND
viewForHeaderInSection
Here is some working code in Swift 3
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderTitleArray[section]
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let returnedView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 25))
returnedView.backgroundColor = .lightGray
let label = UILabel(frame: CGRect(x: 10, y: 7, width: view.frame.size.width, height: 25))
label.text = self.sectionHeaderTitleArray[section]
label.textColor = .black
returnedView.addSubview(label)
return returnedView
}
SWIFT 5
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId") as! CustomHeader
let background = UIView(frame: view.bounds, background: .clear)
header.backgroundView = background
return header
}
SWIFT 4
Super easy, by setting header's view, contentView background color..
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId") as! CustomHeader
header.contentView.backgroundColor = AnyColor
return header
}
Swift 5 iOS 13:
//Remove 'override' if you don't override from UITableviewController
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let headerView = view as? UITableViewHeaderFooterView else { return }
headerView.tintColor = .clear //use any color you want here .red, .black etc
}
Swift 4.X
This is tested and working code.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Total Count (41)"
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor.lightGray
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.darkGray
header.textLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
}
Swift 3+
I used this:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 20))
headerView.backgroundColor = .lightGray
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = stringValues[section]
headerView.addSubview(label)
label.leftAnchor.constraint(equalTo: headerView.leftAnchor).isActive = true
label.rightAnchor.constraint(equalTo: headerView.rightAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true
label.heightAnchor.constraint(equalToConstant: 25).isActive = true
return headerView
}
Swift 5
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let headerView = view as? UITableViewHeaderFooterView else { return }
headerView.backgroundView?.backgroundColor = .red
}
And, if you want different header background (and/or text) colors for each section:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let headerView = view as? UITableViewHeaderFooterView else { return }
switch section {
case 0:
headerView.backgroundView?.backgroundColor = .red
headerView.textLabel?.textColor = .white
case 1:
headerView.backgroundView?.backgroundColor = .green
headerView.textLabel?.textColor = .white
case 2:
headerView.backgroundView?.backgroundColor = .yellow
headerView.textLabel?.textColor = .black
// etc
default:
return
}
Swift5
Versión updated to iOS 11,12,13,14 with example
change background color and text color:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let tableView = view as? UITableViewHeaderFooterView else { return }
tableView.textLabel?.textColor = UIColor.white
tableView.contentView.backgroundColor = UIColor.black
}
Full example of viewcontroller:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
private let array: [String] = ["ab","bc","cd","de","ef","fg","gh","hi","ij","jk"]
let sectionHeaderTitleArray = ["test1","test2", "test3"]
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 11, *) {}
else {
self.edgesForExtendedLayout = []
}
configTableview()
}
private func configTableview() {
tableView.registerCell(type: SyncCell.self)
tableView.separatorColor = #colorLiteral(red: 0, green: 0.3066673801, blue: 1, alpha: 0.19)
tableView.delegate = self
tableView.dataSource = self
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let tableView = view as? UITableViewHeaderFooterView else { return }
tableView.textLabel?.textColor = UIColor.white
tableView.contentView.backgroundColor = UIColor.black
}
func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
return sectionHeaderTitleArray[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 130
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected cell: \(indexPath)")
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = array[indexPath.row]
let cell = tableView.dequeueReusableCell(type: SyncCell.self, forIndexPath: indexPath)
cell.configCell(text: item)
cell.selectionStyle = .none
return cell
}
}
If you are using titleForHeaderInSection, then the easiest way is to add in viewDidLoad():
self.tableView.backgroundColor = UIColor.clear
For some reason, setting ClearColor for Background in the View section for the TableView in the Interface Builder doesnt always set it to transparent. But adding this one line in the code (for me at least) does.
override viewForHeaderInSection and create UITableViewHeaderFooterView
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var headrview = tableView.dequeueReusableHeaderFooterView(withIdentifier: "aaa")
if headrview == nil {
headrview = UITableViewHeaderFooterView(reuseIdentifier: "aaa")
}
let bview = UIView()
bview.backgroundColor = Theme.current.navigationColor
headrview?.backgroundView = bview
headrview?.textLabel?.textColor = Theme.current.primaryTextColor
return headrview
}
Swift 4 solution.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView:UIView = UIView()
headerView.backgroundColor = UIColor.lightGray
return headerView
}
If you have a custom header view, you can do it with contentView.backgroundColor.
There is no need to use hacks such as willDisplayHeaderView method or others.
Simply create your own class of UITableViewHeaderFooterView.
Then in func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? return your created custom header view. You should change background color not directly for your header view but for contentView of your header view 🙂
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: NotificationSettingsHeaderView.reuseIdentifier) as! NotificationSettingsHeaderView
headerView.label.text = dataSource.snapshot().sectionIdentifiers[section].title
headerView.contentView.backgroundColor = .red
return headerView
What's even better is to setup background color in init of your header view subclass
This will change it for all the headers in your application :
UITableViewHeaderFooterView.appearance().backgroundColor = theme.subViewBackgroundColor
override or implement the function tableview will display
public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView = (view as? UITableViewHeaderFooterView)
headerView?.tintColor = .clear
}
We can change the footer color in a section of tableView using the below code snippet.
call the Method heightForFooterInSection
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 5.0
}
call the method viewForFooterInSection
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let viw = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 5))
viw.backgroundColor = .white
return viw
}
Hope this will help you. This is a tested Code.

iOS Swift viewForHeaderInSection Not being Called

I have a UITableview I am trying to add a HeaderView onto my UITableview .
However the viewForHeaderInSection is not being called, but I see the titleForHeaderInSection is being called or is shown. I also tried using a custom cell header for this and it also does not work.
I have no idea what I am doing wrong.
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Groceries"
tableView.registerNib(UINib(nibName: "TransactionSpecifiedCategoriesTableViewCell", bundle: nil), forCellReuseIdentifier: "TransactionSpecifiedCategoriesTableViewCell")
tableView.registerNib(UINib(nibName: "TransactionSpecifiedCategoriesHeaderViewCell", bundle: nil), forHeaderFooterViewReuseIdentifier: "TransactionSpecifiedCategoriesHeaderViewCell")
tableView.tableFooterView = UIView(frame: CGRectMake(0, 0, 0, 0))
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 100))
footerView.backgroundColor = UIColor.blackColor()
return footerView
//let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
//var headerView = UIView(frame: CGRectMake(0, 0, 100, 320))
//headerView.backgroundColor = UIColor.blackColor()
//
//return headerView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 200.0
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "First section header title"
}
In swift 3
You should call
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let rect = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 44)
let footerView = UIView(frame:rect)
footerView.backgroundColor = UIColor.clear
return footerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
The viewForHeaderInSection method belongs to the UITableViewDelegate Protocol. You therefore must set the delegate of your table view to your view controller in order to get this callback. You probably did just set the tableview's dataSource as your other methods are called.
in Swift 3.0
you can also add tableView.estimatedSectionHeaderHeight = 40 into the viewDidLoad to get viewForHeaderInSection called
I had this issue with viewForHeaderInSection not being called in the UITableViewController, which is the delegate by default. It turned out that
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
was preventing viewForHeaderInSection from being called. Thus, if the delegate thing doesn't work, you may need to check that you aren't overriding one of the other section header methods.

Resources