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

How can I add an UISwitch to the section of an UITableView?

我想将 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
    }
}

开关本身出现了,但它没有改变它的状态。 你知道如何解决我的问题吗?

我刚刚获取了您的代码并将方法替换为 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