0

我一直在寻找这个答案,但似乎找不到。我在集合视图上方有一个 sectionHeader。我在标题中添加了一个按钮,但该功能没有运行。介意快速浏览 1 分钟并帮助我吗?

import UIKit
class SearchHeader: UICollectionViewCell {

    let filterLbl: UILabel = {
        let label = UILabel()
        label.text = "Filter"
        label.font = UIFont.systemFont(ofSize: 18)
        return label
    }()

    let addFilterBtn: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "addFilter").withRenderingMode(.alwaysOriginal), for: .normal)
        button.addTarget(self, action: #selector(handleAddFilter), for: .touchUpInside)
        return button

    }()

    func handleAddFilter(button: UIButton) {
        print(1234)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .white

        addSubview(filterLbl)
        addSubview(addFilterBtn)

        filterLbl.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: nil, paddingTop: 10, paddingLeft: 10, paddingBottom: 10, paddingRight: 0, width: 0, height: 0)
        addFilterBtn.anchor(top: topAnchor, left: filterLbl.rightAnchor, bottom: bottomAnchor, right: nil, paddingTop: 10, paddingLeft: 5, paddingBottom: 10, paddingRight: 0, width: 0, height: 0)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
4

1 回答 1

0

我找到了答案。

lazy var addFilterBtn: UIButton = {
    let button = UIButton(type: .system)
    button.setImage(#imageLiteral(resourceName: "addFilter").withRenderingMode(.alwaysOriginal), for: .normal)
    button.addTarget(self, action: #selector(handleAddFilter), for: .touchUpInside)
    return button

}()

出于某种原因,您必须将 let 更改为惰性 var

于 2017-09-30T20:37:10.197 回答