How can I make the footerview always stay at the bottom in UITableViewController?

前端 未结 4 1982
日久生厌
日久生厌 2021-02-13 20:56

I am using a UITableviewcontroller and I want the footer view to always stay at the bottom of the screen. However, the y

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-13 21:12

    You cannot use a UITableViewController, but it's not necessary anyway. You can easily add a UITableView to a regular UIViewController, and just use auto layout to add a UIView below the UITableView. Here's a simple example, adding a red UIView below the UITableView:

    import UIKit
    
    class MyFooterTableViewController: UIViewController {
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
        var data = [ "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"]
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.tableView.dataSource = self
        } 
    
        @IBOutlet weak var tableView: UITableView!
    
    }
    
    extension MyFooterTableViewController: UITableViewDataSource {
        // MARK: - Table view data source
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return data.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableCellTest", for: indexPath)
    
            cell.textLabel?.text = data[indexPath.row]
            cell.detailTextLabel?.text = "BANANA"
            return cell
        }
    }
    

    The constraints on the Storyboard look like this:

    The end result is this:

    Full project code is here:

    https://www.dropbox.com/s/pyp42nzumquompp/TableViewFooter.zip?dl=0

提交回复
热议问题