【问题标题】:How can I add an UISwitch to the section of an UITableView?如何将 UISwitch 添加到 UITableView 的部分?
【发布时间】:2018-05-07 13:20:17
【问题描述】:

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

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

【问题讨论】:

  • 不回复是什么意思?没有识别到​​触摸?
  • 是的,它并没有改变它的状态
  • 你能把你的整个titleForHeaderInSection委托方法贴出来吗?
  • 我添加了整个委托方法。

标签: ios swift uitableview uiswitch uitableviewsectionheader


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 2019-08-09
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2021-04-04
    相关资源
    最近更新 更多