如何将UISwitch添加到UITableView的部分?

时间:2018-05-07 13:20:17

标签: ios swift uitableview uiswitch uitableviewsectionheader

我想将UISwitch放在UITableView的部分。我已经把Switch本身放到了这个部分;但它没有回应。
titleForHeaderInSection 中,我将视图加载为 UITableViewHeaderFooterView 。之后,我手动添加我的子视图,如UIImage和我的UISwitch:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let key = Array(indexNames.keys)[section];
    view.translatesAutoresizingMaskIntoConstraints = false;
    let header = view as! UITableViewHeaderFooterView;
    header.isUserInteractionEnabled = true

    if let imageResource =  (indexNames[key]![0] as! SectionItem).getIconResource() {

        let headerImageView = UIImageView(frame: CGRect(x: 8, // header.contentView.frame.origin.x,
                                                        y: header.frame.origin.y + header.bounds.height / 2 - 18, // header.contentView.frame.origin.y,
                                                        width: 36, height: 36))
        let headerImage = UIImage(named: imageResource)
        headerImageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
        headerImageView.image = headerImage;
        headerImageView.clipsToBounds = true
        header.contentView.addSubview(headerImageView);

        let headerText = UILabel(frame: CGRect(x: 52, y: header.frame.origin.y + header.bounds.height / 2 - 10, width: 100, height: 20));
        // headerText.translatesAutoresizingMaskIntoConstraints = false
        headerText.textColor = UIColor.blue;
        headerText.text = key;
        header.contentView.addSubview(headerText);

        let groupSwitch = UISwitch(frame: CGRect(x: header.bounds.width - 132, y: header.frame.origin.y + header.bounds.height / 2 - 10, width: 100, height: 20));
        groupSwitch.isEnabled = true
        groupSwitch.isUserInteractionEnabled = true
        header.contentView.addSubview(groupSwitch)
        header.contentView.bringSubview(toFront: groupSwitch)
        header.contentView.isUserInteractionEnabled = true

        // view.;
        NSLayoutConstraint(item: headerImageView, attribute: .leading, relatedBy: .equal, toItem: header, attribute: .leading, multiplier: 1.0, constant: 16.0).isActive = true
        NSLayoutConstraint(item: headerImageView, attribute: .top, relatedBy: .equal, toItem: header, attribute: .top, multiplier: 1.0, constant: 8).isActive = true;
        NSLayoutConstraint(item: headerImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 24).isActive = true
        NSLayoutConstraint(item: headerImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 24).isActive = true



        NSLayoutConstraint(item: headerText, attribute: .leading, relatedBy: .equal, toItem: headerImageView, attribute: .trailing, multiplier: 1.0, constant: 16).isActive = true
        NSLayoutConstraint(item: headerText, attribute: .top, relatedBy: .equal, toItem: header, attribute: .top, multiplier: 1.0, constant: 8).isActive = true;
        NSLayoutConstraint(item: headerText, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 150.0).isActive = true
        NSLayoutConstraint(item: headerText, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 20).isActive = true
    }
}

交换机本身正在显示,但它没有改变它的状态。 你知道如何解决我的问题吗?

1 个答案:

答案 0 :(得分:1)

我刚接受了您的代码,并将方法替换为viewForHeaderInSection

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    header.isUserInteractionEnabled = true

    let groupSwitch = UISwitch(frame: CGRect(x: 0, y: 0, width: 100, height: 20));
    groupSwitch.isEnabled = true
    groupSwitch.isUserInteractionEnabled = true
    header.addSubview(groupSwitch)
    return header
}

它完美无缺。

您可以根据需要自定义视图,在返回委托之前添加您想要的内容。

如果您需要以相同的方式放在页脚上,但使用viewForFooterInSection