在tableViewHeader中添加一个按钮,如默认的iOS之一

时间:2019-03-06 12:33:59

标签: ios swift uitableview

我有一个UITableViewController,我想在其中添加一个UIButton到节标题中。

我在日历应用程序中看到了此按钮,我想知道这是否是我可以重用的类的一部分,或者是否需要编写自己的类来添加它。

Button in tableView header in calendar app

2 个答案:

答案 0 :(得分:2)

在这种情况下,您需要使用自己的自定义类。 因为 UITableViewHeaderFooterView 本身不包含任何类型的按钮。 因此,最好使用customClass。

答案 1 :(得分:0)

您必须创建一个xib文件并创建它的类文件。 添加您的按钮并创建其插座。 在viewDidLoad()中注册您的xib相同

例如:

let headerNib = UINib(nibName: "yourNIBname", bundle: nil)
tableView.register(headerNib, forHeaderFooterViewReuseIdentifier: "reuseIdentifer")

然后添加以下tableview委托

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
        let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "reuseIdentifer") as! youNIBClassName
        headerView.yourButton.addTarget(self, action: #selector(hideAllTapped(withSender:)), for: .touchUpInside)
        return headerView
    }

您将在hideAllTapped()函数中获得操作事件

@objc func hideAllTapped(withSender sender: UIButton) {
        // do your action
    }
相关问题