繁体   English   中英

无法将 UIButton 添加到 tableView 部分

[英]cannot add UIButton to tableView section

我有这个代码

 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 45))
        headerView.backgroundColor = UIColor.white

        let label = UILabel(frame: CGRect(x: 0, y: 12.5, width: tableView.frame.width - 100, height: 20))
        label.numberOfLines = 0
        label.font = UIFont(name: "SFProDisplay-Bold", size: 18);
        label.textColor = UIColor.darkGray

        let sectionName: String
        switch section {
            case 0:
                sectionName = NSLocalizedString("   Featured", comment: "featured")
            case 1:
                sectionName = NSLocalizedString("   Trending", comment: "trending")
            // ...
            default:
                sectionName = NSLocalizedString("   New", comment: "new")
        }
        label.text = sectionName
        label.adjustsFontForContentSizeCategory = true
        headerView.addSubview(label)

        let button:UIButton = UIButton(
            frame: CGRect(x: tableView.bounds.size.width-100,
                          y: 12.5,
                          width: 100,
                          height: 20))
        button.setTitle("More", for: .normal)
        button.titleLabel?.textColor = UIColor.black
        button.backgroundColor = UIColor.white
//        button.addTarget(self, action: #selector(btnShowHideTapped), for: .touchUpInside)

        button.titleLabel?.textColor = UIColor.black
        headerView.addSubview(button)

        return headerView
    }

我正在尝试将 UIButton 添加到 tableView 标头,但它不起作用。 有谁知道为什么?

UIButton 不显示标签的原因。

此行不起作用:

button.titleLabel?.textColor = UIColor.black

将其替换为以下行:

button.setTitleColor(.black, for: .normal)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM