【问题标题】:Tableviews delegate eats button clickTableviews 代表吃按钮点击
【发布时间】:2019-03-18 13:37:21
【问题描述】:

我有一个大的 UITableViewCell 代表一个地方。单元格的元素是两个按钮 starButtoninfoButton。他们两个都有一个永远不会执行的targetAction,我不知道为什么......所有的触摸都被tableview的didSelectRowAt吃掉了。让我如此困惑的是几个小时前它一切正常,我没有更改两个按钮的属性。有人知道发生了什么吗?

层次结构如下所示:

UITableView

OverviewTableCell UITableViewCell

dotsButton, thumbnailImageView UIButton , UIImageView

blackViewUIView

titleLabel、descriptionLabel、starButton、infoButton UILabelUIButton


单元格:

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()函数显示frame和bounds没问题(我猜的)

(0.0, 0.0, 382.0, 215.0) (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)

【问题讨论】:

    标签: ios swift uitableview uibutton uigesturerecognizer


    【解决方案1】:

    你可以试试

    blackView.isUserInteractionEnabled = true
    

    也将子视图添加到

    self.contentView 并用它创建单元格子视图的约束

    【讨论】:

    • blackView.isUserInteractionEnabled = true 解决了这个问题!谢谢!使用 contentView 的目的是什么?
    猜你喜欢
    • 1970-01-01
    • 2013-05-18
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2015-02-22
    相关资源
    最近更新 更多