sectionHeaderView中的UIButton不适用于所有部分

时间:2017-06-26 10:31:59

标签: ios swift3 uibutton tableview uitableviewsectionheader

我有tableView,有5个部分。 每个部分都有一个在viewForHeaderInSection委托方法中创建的节标题。节标题有buttonlabel,它们完美显示。按钮有一个目标函数,适用于0部分,1,2但不适用于第3和第4部分。 请注意tableview使用四个原型单元格,使得0,1,4部分使用不同的单元格,部分2和3使用相同的单元格。 我也尝试使用xib创建节标题但面临同样的问题。还为xib和tableview中的所有单元格启用了用户交互。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    if section == 0 {
        return 36.0
    }
    return 46.0


}   

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

    if section == 0 {

        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 36))

        let sectionTitleLabel : UILabel = {

            let label = UILabel()
            label.text = sectionHeaderTitles[section]
            label.font = UIFont(name: "HelveticaNeue-Medium", size: 16.0)
            label.translatesAutoresizingMaskIntoConstraints = false
            return label
        }()

        view.addSubview(sectionTitleLabel)

        view.addConstraintsWithFormat(format: "H:|-10-[v0(100)]", views: sectionTitleLabel)
        view.addConstraintsWithFormat(format: "V:|-8-[v0(20)]", views: sectionTitleLabel)

        if self.flag == 0{

            let editButton : UIButton = {

                let button = UIButton()
                button.setImage(UIImage(named: "EditGrey"), for: .normal)
                button.addTarget(self, action: #selector(ProfileViewController.editButtonPressed(sender:)), for: .touchUpInside)
                button.tag = section
                button.translatesAutoresizingMaskIntoConstraints = false
                return button
            }()

            view.addSubview(editButton)
            view.addConstraintsWithFormat(format: "H:[v0(20)]-10-|", views: editButton)
            view.addConstraintsWithFormat(format: "V:|-8-[v0(20)]", views: editButton)

        }

        return view

    }

    else{

        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 46))

        let separator : UIView = {

            let view = UIView()
            view.backgroundColor = UIColor(hex: "D3D3D3")
            view.translatesAutoresizingMaskIntoConstraints = false
            return view

        }()

        let sectionTitleLabel : UILabel = {

            let label = UILabel()
            label.text = sectionHeaderTitles[section]
            label.font = UIFont(name: "HelveticaNeue-Medium", size: 16.0)
            label.translatesAutoresizingMaskIntoConstraints = false
            return label
        }()

        view.addSubview(separator)
        view.addSubview(sectionTitleLabel)
        view.addConstraintsWithFormat(format: "H:|[v0]|", views: separator)
        view.addConstraintsWithFormat(format: "V:|[v0(10)]", views: separator)
        view.addConstraintsWithFormat(format: "H:|-10-[v0(100)]", views: sectionTitleLabel)
        view.addConstraintsWithFormat(format: "V:[v0]-8-[v1(20)]", views: separator, sectionTitleLabel)

        if self.flag == 0 {

            let editButton : UIButton = {

                let button = UIButton()
                button.setImage(UIImage(named: "EditGrey"), for: .normal)
                button.tag = section
                button.addTarget(self, action: #selector(ProfileViewController.editButtonPressed(sender:)), for: .touchUpInside)
                button.translatesAutoresizingMaskIntoConstraints = false
                return button
            }()

            view.addSubview(editButton)
            view.addConstraintsWithFormat(format: "H:[v0(20)]-10-|", views: editButton)
            view.addConstraintsWithFormat(format: "V:[v0]-8-[v1(20)]", views: separator, editButton)

        }

        return view
    }

}

第0节的视图没有分隔符,这就是为什么它是单独创建的。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

由于tableview的容器视图的高度小于tableview内容的高度,因此按钮不起作用的原因。所以落在容器视图之外的按钮不起作用。