color for section headers in UITableView - ios

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?

Related

swift change section array element text or font color in tableview header

My tableview code is -
import UIKit
class ViewController: UIViewController {
var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"]
}
extension ViewController : UITableViewDelegate { }
extension ViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return categories.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
return cell
}
}
The result on the simulator is -
Now, I want the to customize the font/text of the various sections i.e. Action, Drama, Science Fiction, Kids, Horror other than black, may be make it bigger etc. How is it possible ?
With tableView(_:titleForHeaderInSection:) method, you can't modify the UI properties of the header text.
You need to implement tableView(_:viewForHeaderInSection:) and tableView(_:heightForHeaderInSection:) methods to get a custom UI for the header, i.e.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.text = categories[section]
label.font = UIFont.systemFont(ofSize: 20.0, weight: .bold)
label.textColor = .red
label.sizeToFit()
return label
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50.0
}
Simply return a UILabel instance with required text attributes in tableView(_:viewForHeaderInSection:)
There is below function available in UITableViewDelegate
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
<#code#>
}
You would have the full control over the header of each section and customisation can be done accordingly.
In the method
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
the table view uses a fixed font style for section header titles. If you want a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView(_:viewForHeaderInSection:) instead.
You can use delegate method of UITableView tableView(_:viewForHeaderInSection:) to get custom UI for the header
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: yourTable.bounds.width, height: 40))
let headerLabel = UILabel(frame: CGRect(x: 15, y: 0, width: yourTable.bounds.width, height: 40))
headerLabel.font = UIFont.boldSystemFont(ofSize: 20)
headerLabel.textColor = .blue
headerLabel.text = self.tableView(self.yourtableView, titleForHeaderInSection: section)
headerLabel.sizeToFit()
headerView.addSubview(headerLabel)
return headerView
}
And height for section
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30 //whatever you want
}

How to remove the separator between the header and the first row in a grouped tableView

I am creating a static tableView. My goal is to have a single view that displays a tableView with 3 rows (get support, send feedback, participation terms), and a header + footer.
Right now, it all works fine EXCEPT the fact that there are two extra separators (one between the header and the first cell and the other between the last cell and the footer) that I cannot seem to get rid of.
Here is my code:
final class viewController: UITableViewController {
private let headerContainer = UIView()
private let footerContainer = UIView()
private let tableData = ["Get Support", "Send Feedback", "Participation Terms"]
override func viewDidLoad() {
super.viewDidLoad()
setupHeaderAndFooter()
setupTableView()
}
func setupHeaderAndFooter() {
/* setup code here (not relevant to this question) */
func setupTableView() {
// reinitializing tableView so that we can change its style to grouped
tableView = UITableView(frame: CGRect.zero, style: .grouped)
tableView.delegate = self
tableView.separatorStyle = .singleLine
}
//MARK: UITableView Methods
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return headerContainer
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 150
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return footerContainer
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 200
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
cell.textLabel!.text = tableData[indexPath.row]
return cell
}
}
One way to achieve this is don't use default separator provided by UITableView. and place one UILabel or UIView and set its background color to light gray and set its height=1 (As per your requirement), width (full tableview width or as per your requirement) and position at bottom of all you content inside cell, header or footer.
Ok here you go.
The easy trick is actually a one-liner and a self-explanatory:
self.tableView.separatorColor = self.tableView.backgroundColor
AFTER:
I hope this helps!
You have to set clipsToBounds to true on your header view.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = //Get your header here
header.clipsToBounds = true
return header
}
Before:
After:

update text on uitableview header

I am using the code below to set the uitableview viewForHeaderInSection and then I am trying to update just one textfield in the section header but my reference is nil.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ReusableHeader")
header.timeField.text = String(describing: secondsToHoursMinutesSeconds(seconds: counter))
return header
}
My reference code where header comes back as nil:
let header = self.tableView.headerView(forSection: 0)
header?.setNeedsLayout()
Ok , i'am researching about this thing. func self.tableView.headerView(forSection: ) return only UITableViewHeaderFooterView object. If you want use this func , you need use tableView.dequeueReusableHeaderFooterView(withIdentifier: "ReusableHeader")
public override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ReusableHeader")
return view
}

How to set a custom cell as header or footer of UITableView

I am using Xib files instead of storyboard. I have created a table view with some dummy data. Its working fine, Now I have created a custom cell with some labels and textFields. How can I use it as header or footer of UITableView ?
The answer is actually pretty simple.
In viewForHeaderInSection() create cell object as you do in cellForRowAtIndexPath() and return it.
Here is sample code.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as! YourTableViewCell
return cell
}
You can set height for header as:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80
}
Update for Swift 4.2
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") as! YourTableViewCell
return cell
}
and
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80
}
You can create a custom view programmatically. I tried the following code. Try it once.
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let customview:UIView = UIView();
let label = UILabel();
label.text = "ur custom data"
/*
You can add any number of subviews you want here
And don't forget to add it to your customview.
*/
customview.addSubview(label)
return customview;
}
similarly you can do the same thing for header also.
This is the Method add the Header of TableView:
In that you can any controls Like UIButton,UIImageView,....etc.
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView = UIView()
sectionView.frame = CGRectMake(x,y,height,width) // For Set the Frame
sectionView.backgroundColor = UIColor.redColor()
return sectionView
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let sectionView = UIView()
sectionView.frame = CGRectMake(x,y,height,width) // For Set the Frame
sectionView.backgroundColor = UIColor.redColor()
return sectionView
}
Like Above you can set the Footer of Section and
You can add the size of the header And footer
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20.0
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20.0
}
Swift 4.0
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView = UIView()
sectionView.frame = CGRect(x: x, y: y, width: width, height: height)// For Set the Frame
sectionView.backgroundColor = UIColor.red
return sectionView
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let sectionView = UIView()
sectionView.frame = CGRect(x: x, y: y, width: width, height: height)// For Set the Frame
sectionView.backgroundColor = UIColor.red
return sectionView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20.0
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20.0
}

`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

Resources