Adding UIButton to UITableView section header

前端 未结 7 1142
一个人的身影
一个人的身影 2021-02-01 20:20

I have a UITableView with 1 section and for the section header, I would like to keep everything about the header the same but simply add a button on the right side.

7条回答
  •  长发绾君心
    2021-02-01 20:54

    If you want to use Interface Builder to build your section header, you can use a UITableViewCell as the header. Create a prototype cell for your header in your UITableView, customize as you would any other cell, including adding labels, buttons, and constraints.

    You can instantiate in your UITableViewController lazily:

    lazy var headerCell: MyHeaderTableViewCell = {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Header") as! MyHeaderTableViewCell
        return cell
    }()
    

    To make it the header:

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return headerCell
    }
    

提交回复
热议问题