gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 14:46:28 25 4
gpt4 key购买 nike

我想将 UISwitch 放置到 UITableView 的部分。我已经把 Switch 本身放到了这个部分;但它没有回应。
titleForHeaderInSection 中,我将 View 加载为 UITableViewHeaderFooterView。之后我手动添加我的 subview ,比如 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
}

它完美地工作。

您可以根据需要自定义您的 View ,在返回委托(delegate)之前在其中添加您想要的任何内容。

如果您需要放置在页脚上,方法相同,但使用 viewForFooterInSection

关于ios - 如何将 UISwitch 添加到 UITableView 的部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50215260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com