Separators in table views(Swift) - ios

How can I add these separators or blank spaces between the tableview cells?
Or you just use two different cells?

you can add section footer view
Code:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
return footerView
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 50
}

Related

How to add separators between UITableView sections?

I'm implementing a tableview, which has 3 sections(section count remain static) and each section contain several rows.
Behaviour 1: Add section header to only the third section.
How did I achieve:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 2 {
//create and return header view
}
return UIView(frame: CGRect(x: 0, y:0, width: tableView.width, height: UIScreen.main.scale))
}
Behaviour 2: TableViewCells shouldn't have separator. But, Sections should be separated with a thin line. (similar to tableviewcell separator)
What did I try:
Add a one pixel height, tableView width UIView, with all required properties set(color, etc) and add it as a sub-view to the section header view's
Add drop shadow with CGPoint(0,1) to the section header views
None of them worked out.
How did I achieve this:
Find last cell in every section,
Create an UIView with 1px height, tableView's width and add it as a subview to the last cell
Though this seem to work. I feel, there must be a better way of doing this, rather than adding line to bottom of every section's last tableView cell. Does anyone know a better way of implementing such behaviour?
Set the Style of the Table View to Grouped:
Using heightForHeaderInSection method of UITableViewDelegate and return section wise height as per you want
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 2 {
return 1 // return height as per your requirement
}
return 0 // return height as per your requirement
}
This should work for adding "section separator lines" and a head view only on the 3rd section:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// if it's the 3rd section, return a view with desired content for the section header
if section == 2 {
let v = UIView(frame: CGRect(x: 0, y:0, width: tableView.frame.width, height: 30))
v.backgroundColor = .yellow
let label = UILabel(frame: CGRect(x: 8.0, y: 4.0, width: v.bounds.size.width - 16.0, height: v.bounds.size.height - 8.0))
label.autoresizingMask = [.flexibleWidth, .flexibleHeight]
label.text = "Header for Third Section"
v.addSubview(label)
return v
}
// not the 3rd section, so don't return a view
return nil
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
// if it's the 3rd section
if section == 2 {
return 30
}
return 0
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
// UIView with darkGray background for section-separators as Section Footer
let v = UIView(frame: CGRect(x: 0, y:0, width: tableView.frame.width, height: 1))
v.backgroundColor = .darkGray
return v
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
// Section Footer height
return 1.0
}
Simple solution of this Just add the footer view inside the tableview.Set height of the footer view to be a specific height.
Swift:
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = UIColor.grey
return view
}
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}

Change font of a Table Header Section

Can anyone help me with the code missing in viewForHeaderInSection?
Do I have to create a UILabel?
What exactly should be done to change the font of the Table Header Section?
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 55.0;//Choose your custom row height
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.hoursArray.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
//Code missing!
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.hoursArray[section]
}
Remove this function and it should work with default label.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
}
For custom header, You can return any view here is an example of a UIlabel.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
let label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.size.width, height: 30))
label.textColor = UIColor.red
label.text = self.hoursArray[section]
return label
}
Do not implement both viewForHeaderInSection and titleForHeaderInSection. If you want a custom view for the header, just implement the viewForHeaderInSection method. And you also need to implement the heightForHeaderInSection otherwise the header won't appear.
Typically you create, setup, and return an instance of UITableViewHeaderFooterView but you can also simply create a UILabel setup as desired.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView
{
let label = UILabel(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.size.width, height: 30)
label.text = "I'am a test label"
label.font = UIFont(name: "Arial", size: label.font.pointSize)
return label
}

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
}

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

Resources