简体   繁体   中英

Custom UITableView Cell inside a UITableViewController footer

I have a UITableviewController which I created programmatically in Swift4. 在此处输入图片说明

The marked rectangle in the picture is my footerview with a UITableView inside

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        let tableFooterView = UITableView()
        tableFooterView.rowHeight = 100

        return tableFooterView
    }

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 365
    }

I want to load custom cells in the tableview cells in footerview. I tried using this following code

 tableFooterView.dequeueReusableCell(withIdentifier: emptyCellIdentifier) as! EmptyTableViewCells

But I get error -

fatal error: unexpectedly found nil while unwrapping an Optional value

Please help

You have to register the cell for the footer tableView when you are creating in viewForFooterInSection method and you should distinguish both the tables via tag or something if you are making the delegate and datasource as self for the tableFooterView UITableView

tableFooterView.register(EmptyTableViewCells.self, forCellReuseIdentifier: emptyCellIdentifier)

So your method will be :

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    if tableView.tag == 1 {
        tableFooterView = UITableView()
        tableFooterView.register(EmptyTableViewCells.self, forCellReuseIdentifier: emptyCellIdentifier)
        tableFooterView.rowHeight = 100
        tableFooterView.tag = 2
        tableFooterView.dataSource = self
        tableFooterView.delegate = self
        return tableFooterView
    } else {
        //footer view is nil for the table view which is in the main table footer
        return nil
    }
}

This will stop the crash on the tableFooterView.dequeueReusableCell line but you need to add the other views like buttons and labels on the tableFooter Empty Cell by code and not IBOutlet. It will again crash if you access any IBOutlet connected component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM