Swift Add Footer View in Uitableview

Swift Add Footer View In UITableView

Using StoryBoard

In UITableView You can drag UIView, it will set as FooterView if you have more then 0 prototype cell. After Drag you can see it in table view hierarchy as a subview. Now, you can add the label button on that View, you can also set IBAction into ViewController Class File.

Programmatically

Follow 3 Steps

  1. Make one custom view with button,

Swift 3.X / Swift 4.X

let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
customView.addSubview(button)

Swift 2.X

let customView = UIView(frame: CGRectMake(0, 0, 200, 50))
customView.backgroundColor = UIColor.redColor()
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", forState: .Normal)
button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
customView.addSubview(button)

  1. Add that view in Table Footer View.

Swift 2.X/Swift 3.X/Swift 4.X

myTblView.tableFooterView = customView

  1. you can do action on that button in same class.

Swift 3.X/Swift 4.X

@objc func buttonAction(_ sender: UIButton!) {
print("Button tapped")
}

Swift 2.X

func buttonAction(sender: UIButton!) {
print("Button tapped")
}

How to add footer as additional information in table view swift

You can fix your code by setting constraints to the UILabel:

let footerView = UIView()
if section == 1 {
let label = UILabel()
label.text = "Additional information here"
label.font = .systemFont(ofSize: 16)
label.textColor = UIColor.black
label.backgroundColor = UIColor.clear
label.textAlignment = .left
footerView.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.topAnchor.constraint(equalTo: footerView.topAnchor).isActive = true
label.leftAnchor.constraint(equalTo: footerView.leftAnchor).isActive = true
label.rightAnchor.constraint(equalTo: footerView.rightAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: footerView.bottomAnchor).isActive = true
}
return footerView

But it's way simpler to use simply a UILabel, since UILabel is a UIView also, you can return it directly.

How to display custom footer view in table?

Using StoryBoard

In UITableView You can drag UIView, it will set as FooterView if you have more then 0 prototype cell. After Drag you can see it in tableview hierarchy as subview. Now, you can add UILabel UIButton or any component on that View, adjust the height, etc. You can also set IBAction into ViewController Class File.

Programmatically

let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", for: .normal)
customView.addSubview(button)

//Add that view in Table Footer View.
tableView.tableFooterView = customView // Here you can also use your xib View.

By using XIB

class MyClass: UIView {

class func instanceFromNib() -> UIView {
return UINib(nibName: "nib file name", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
}

Initialise the view and use it like below

let FooterView = MyClass.instanceFromNib()
tableView.tableFooterView = FooterView

Sample Image

Output:

Sample Image

How to add a footer to a UITableView in Storyboard

You can just drag a view to the bottom area of the tableView. You'll see in the hierarchy that it will be a subview of the tableview. You can then drag subviews such as labels and buttons there, adjust the height, etc.

Adding footer to UITableView

first add the fotter to tableView then add the Constraints

100% ok tested

self.tableView.tableFooterView = footer

NSLayoutConstraint.activate([ footer.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30), footer.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30), footer.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -60), footer.heightAnchor.constraint(equalToConstant: 90) ])

What is the cleanest way to add footer view to tableView?

i would like to add this to tableview not in section

you are already doing it by setting self.tableView.tableFooterView = footerView

as per docs

Use this property to specify a footer view for your entire table. The
footer view is the last item to appear in the table's view's scrolling
content, and it is separate from the footer views you add to
individual sections. The default value of this property is nil.

link : https://developer.apple.com/documentation/uikit/uitableview/1614976-tablefooterview

So simply remove your viewForFooterInSection code and viewDidLoad of your viewController simply set

override func viewDidLoad() {
super.viewDidLoad()
let footerView: LibraryFooterView = tableView.dequeueReusableHeaderFooter()
footerView.viewModel = LibraryFooterViewModel(navigator: Locator.navigator,
localStorage: Locator.localStorage)
self.tableView.tableFooterView = footerView
}

By writing self.tableView.tableFooterView = footerView in viewForFooterInSection you are simply resetting the same variable agin and again with no actual visual change, and you are simply wasting CPU cycles by creating view and its viewModel again and again. Set it once may be in viewDidLoad or wherever it makes sense in your case and just leave it.

Hope it helps

Set a custom table view footer with Swift

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
}

ios 8 swift - how to add footer for tableview using separate datasource

As noted, displaying headers and footers needs to implement the relevant method of the UITableViewDelegate protocol. For the footer it is:

tableView(tableView: UITableView, viewForFooterInSection section: Int)

Make sure your table view's delegate is set correctly.

Also, note that there is another convenient table view feature: a footer that will be added below the entire table view, regardless of datasource or delegate implementation. Just assign a tableFooterView.



Related Topics



Leave a reply



Submit