代码之家  ›  专栏  ›  技术社区  ›  Noodledew

Tableviews代理按钮单击

  •  -1
  • Noodledew  · 技术社区  · 6 年前

    我有一个很大的UITableViewCell代表一个地方。单元格的元素是两个按钮 星形按钮 信息按钮 . 他们两个都有一个永远不会被执行的目标,我不知道为什么。。所有的触碰都被人吃掉了 didSelectRowAt 在tableview旁边。让我如此困惑的是,它在几个小时前运行良好,我没有改变两个按钮的属性。有人知道发生了什么吗?

    UITableView

    概述TableCell UITableViewCell

    点按钮, UIButton UIImageView

    布莱克维尤 UIView

    UILabel , UI按钮


    单元格:

    class OverviewTableViewCell: UITableViewCell, ReusableView {
    
        lazy var dotsButton: UIButton = {
            let button = UIButton()
            button.setImage(UIImage(named: GSSettings.UI.otherIcons.dotsHorizontal)?.withRenderingMode(.alwaysTemplate), for: .normal)
            button.setImage(UIImage(named: GSSettings.UI.otherIcons.dotsVertical)?.withRenderingMode(.alwaysTemplate), for: .selected)
            button.addTarget(self, action: #selector(seeMore), for: .touchUpInside)
            button.fixHeightAndWidth(width: 28, height: 28)
            button.tintColor = UIColor.gray
            return button
        }()
    
        let thumbnailImageView: UIImageView = {
            let imageview = UIImageView()
            imageview.contentMode = .scaleAspectFill
            imageview.clipsToBounds = true
            imageview.image = UIImage(named: "testbild")
            return imageview
        }()
    
        let blackView: UIImageView = {
            let imageview = UIImageView()
            imageview.backgroundColor = UIColor.black.withAlphaComponent(0.35)
            return imageview
        }()
    
        let titleLabel : UILabel = {
            let label = UILabel()
            label.text = "Titel Titel"
            label.font = GSSettings.UI.Fonts.helveticaMedium?.withSize(22)
            label.textColor = UIColor.white
            return label
        }()
    
        lazy var starButton: GSFavSpotButton = {
            let button = GSFavSpotButton()
            button.tintColor = UIColor.white//GSSettings.UI.Colors.nightOrange
            button.addTarget(self, action: #selector(handleStarButton), for: .touchUpInside)
            return button
        }()
    
        lazy var infoButton: GSInfoButton = {
            let button = GSInfoButton()
            button.tintColor = UIColor.white//GSSettings.UI.Colors.nightOrange
            button.addTarget(self, action: #selector(handleInfoButton), for: .touchUpInside)
            return button
        }()
    
        let addFriendView = GSInviteAFriendView()
    
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.clipsToBounds = false
            self.selectionStyle = .none
            setupSubviews()
            setupConstraints()
            perform(#selector(printBounds), with: nil, afterDelay: 5)
        }
    
        func setupSubviews() {
            self.addSubview(dotsButton)
            self.addSubview(thumbnailImageView)
            thumbnailImageView.addSubview(blackView)
            blackView.addSubview(titleLabel)
            blackView.addSubview(descriptionLabel)
            blackView.addSubview(starButton)
            blackView.addSubview(infoButton)
            self.addSubview(addFriendView)
    
            blackView.isHidden = true
            descriptionLabel.isHidden = true
        }
    
        func setupConstraints() {
            dotsButton.anchor(top: topAnchor, leading: nil, bottom: nil, trailing: trailingAnchor, paddingTop: 0, paddingLeading: 0, paddingBottom: 0, paddingTrailing: 16, width: 0, height: 0)
    
    
            thumbnailImageView.anchor(top: dotsButton.bottomAnchor, leading: leadingAnchor, bottom: nil, trailing: trailingAnchor, paddingTop: 4, paddingLeading: 16, paddingBottom: 0, paddingTrailing: 16, width: 0, height: 0)
            thumbnailImageView.heightAnchor.constraint(equalTo: thumbnailImageView.widthAnchor, multiplier: 9/16).isActive = true
            blackView.fillSuperview(unsafeArea: true)
    
            titleLabel.anchor(top: thumbnailImageView.topAnchor, leading: thumbnailImageView.leadingAnchor, bottom: nil, trailing: starButton.leadingAnchor, paddingTop: 8, paddingLeading: 8, paddingBottom: 0, paddingTrailing: 8, width: 0, height: 20)
    
            infoButton.anchor(top: thumbnailImageView.topAnchor, leading: nil, bottom: nil, trailing: thumbnailImageView.trailingAnchor, paddingTop: 8, paddingLeading: 0, paddingBottom: 0, paddingTrailing: 8, width: 30, height: 30)
            starButton.anchor(top: thumbnailImageView.topAnchor, leading: nil, bottom: nil, trailing: infoButton.leadingAnchor, paddingTop: 8, paddingLeading: 0, paddingBottom: 0, paddingTrailing: 4, width: 30, height: 30)
    
            addFriendView.anchor(top: thumbnailImageView.bottomAnchor, leading: nil, bottom: nil, trailing: nil, paddingTop: -GSSettings.UI.Sizes.addFriendButtonSize/2 + 10, paddingLeading: 0, paddingBottom: 0, paddingTrailing: 0, width: GSSettings.UI.Sizes.addFriendButtonSize, height: GSSettings.UI.Sizes.addFriendButtonSize)
            addFriendView.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
        }
    
        @objc func seeMore() {
            dotsButton.isSelected.toggle()
            if dotsButton.isSelected {
                blackView.isHidden = false
            } else {
                blackView.isHidden = false
            }
            layoutIfNeeded()
        }
    
        @objc func handleStarButton() {
            starButton.isSelected.toggle()
        }
    
        @objc func handleInfoButton() {
            infoButton.isSelected.toggle()
        }
    
        @objc func printBounds() {
            print("::::")
            print(thumbnailImageView.bounds)
            print(infoButton.bounds)
            print(starButton.bounds)
            print("_____")
            print(thumbnailImageView.frame)
            print(infoButton.frame)
            print(starButton.frame)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    extension UIView {
      func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?,  paddingTop: CGFloat, paddingLeading: CGFloat, paddingBottom: CGFloat, paddingTrailing: CGFloat, width: CGFloat, height: CGFloat) {
    
        translatesAutoresizingMaskIntoConstraints = false
    
        if let top = top {
            topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
        }
    
        if let leading = leading {
            leadingAnchor.constraint(equalTo: leading, constant: paddingLeading).isActive = true
        }
    
        if let bottom = bottom {
            bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
        }
    
        if let trailing = trailing {
            trailingAnchor.constraint(equalTo: trailing, constant: -paddingTrailing).isActive = true
        }
    
        if width != 0 {
            widthAnchor.constraint(equalToConstant: width).isActive = true
        }
    
        if height != 0 {
            heightAnchor.constraint(equalToConstant: height).isActive = true
        }
    }
    

    表视图:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: OverviewTableViewCell.reuseIdentifier, for: indexPath) as! OverviewTableViewCell
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        delegate?.pushTo()
    }
    

    单元格中的printBounds()函数显示框架和边界是正确的(我猜)

    (0.0, 0.0, 30.0, 30.0) (0.0, 0.0, 30.0, 30.0)


    (16.0, 32.0, 382.0, 215.0) (344.0, 8.0, 30.0, 30.0) (310.0, 8.0, 30.0, 30.0)

    1 回复  |  直到 6 年前
        1
  •  0
  •   Shehata Gamal    6 年前

    blackView.isUserInteractionEnabled = true
    

    同时将子视图添加到

    self.contentView

    推荐文章