Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios addTarget不适用于UICollectionViewCell中的按钮_Ios_Swift_Uibutton - Fatal编程技术网

Ios addTarget不适用于UICollectionViewCell中的按钮

Ios addTarget不适用于UICollectionViewCell中的按钮,ios,swift,uibutton,Ios,Swift,Uibutton,当我点击按钮时,选择器并没有被调用。 只有两个组件是单元格,一个是图像,另一个是UIButton。 下面是收集单元的代码。是否有其他方法添加方法 class AttachmentCell: UICollectionViewCell { weak var delegate: AttachmentCellDelegate? let removeButton: UIButton = { let button = UIButton(type: .custom) button.tr

当我点击按钮时,选择器并没有被调用。 只有两个组件是单元格,一个是图像,另一个是UIButton。 下面是收集单元的代码。是否有其他方法添加方法

class AttachmentCell: UICollectionViewCell {
   weak var delegate: AttachmentCellDelegate?

let removeButton: UIButton = {
    let button = UIButton(type: .custom)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setImage(UIImage(named: "close_icon"), for: .normal)
    button.addTarget(self, action: #selector(removeButtonTapped), for: .touchUpInside)
    button.isUserInteractionEnabled = true
    return button
}()

let imageView: UIImageView = {
    let imgView = UIImageView()
    imgView.contentMode = .scaleAspectFill
    imgView.clipsToBounds = true
    return imgView
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = UIColor.red
    self.isUserInteractionEnabled = true

    self.contentView.addSubview(imageView)
    imageView.isUserInteractionEnabled = true
    self.contentView.addSubview(removeButton)
    //self.addSubview(removeButton)

    imageView.snp.makeConstraints { (make) in
        make.leading.equalToSuperview()
        make.trailing.equalToSuperview()
        make.top.equalToSuperview()
        make.bottom.equalToSuperview()
    }

    removeButton.snp.makeConstraints { (make) in
        make.width.equalTo(40)
        make.height.equalTo(40)
        make.top.equalTo(imageView.snp.top).offset(-5)
        make.trailing.equalTo(self.imageView.snp.trailing).offset(5)
    }

    self.backgroundColor = UIColor.gray
}

  @objc func removeButtonTapped() {
     delegate?.didRemoveButtonTapped()
  }
}

let removeButton
更改为
lazy var removeButton

self
在调用
init
之前不存在。在
let
常量中将目标添加到
self
时,您是在调用
init
之前定义它的


或者,只需在
init
块中调用
addTarget

这甚至不会编译,也不会。它需要snapKit库。