在UICollectionViewCell中使用时,UIButton交互不流畅

时间:2019-06-11 11:09:21

标签: ios swift uibutton

我有一个UICollectionViewCell,其中添加了UIButton。通常,按钮动作会被调用,但有时不会。当我在视图控制器中添加相同的按钮时,交互非常流畅。轻按一下即可触发动作。 以下是按钮的代码:

 func makeTapButton(for superView: UIView) -> UIButton {
      let offSetValue = 15
      let button = UIButton()
      button.backgroundColor = UIColor.yellow
      superView.addSubview(button)
      button.snp.makeConstraints { (make) in
         make.leading.equalToSuperview().offset(-offSetValue)
         make.trailing.equalToSuperview().offset(offSetValue)
         make.top.equalToSuperview().offset(-offSetValue)
         make.bottom.equalToSuperview().offset(offSetValue)
     }
     return button
 }

   func setupCustomView() {
     self.addSubview(containerStackView)
     containerStackView.snp.makeConstraints { (make) -> Void in
        make.top.equalTo(self)
        make.leading.equalTo(self)
        make.trailing.equalTo(self)
        make.bottom.equalTo(self)
    }

    containerStackView.addArrangedSubview(commentStack)
    containerStackView.addArrangedSubview(retweetStack)
    containerStackView.addArrangedSubview(likeStack)
    commentStack.addArrangedSubview(commentImageView)
    commentStack.addArrangedSubview(commentsCountLabel)
    retweetStack.addArrangedSubview(retweetImageView)
    retweetStack.addArrangedSubview(retweetCountLabel)
    likeStack.addArrangedSubview(likeImageView)
    likeStack.addArrangedSubview(likesCountLabel)

    likeButton = makeTapButton(for: likeStack)
    commentButton = makeTapButton(for: commentStack)
    retweetButton = makeTapButton(for: retweetStack)
}

2 个答案:

答案 0 :(得分:0)

使用放置在collectionview中的UIbutton时尝试以下提到的代码

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! UICollectionViewCell
        cell.btnName.addTarget(self, action: #selector(btnSelClk), for: .touchUpInside)
        cell.binSel.tag = collectionView.tag
        cell.binSel.accessibilityValue = String(indexPath.row)

        return cell
    }

@objc func btnSelClk(sender:UIButton) {
        selectAry[sender.tag] = sender.accessibilityValue!
         // your button action
}

答案 1 :(得分:0)

在UICollectionViewCell类中定义按钮,并在UIViewController类中定义函数,因为它们已被重用,所以它们不会那么麻烦;

import UIKit

class YourCell: UITableViewCell {

@IBOutlet weak var yourBtn: UIButton!

var yourButtonAction: (() -> ())?


 @IBAction func buttonPressed(_ sender: UISlider) {

        yourButtonAction()
    }

}

然后在ViewController中调用单元格;

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCell", for: indexPath) as! YourCell

cell.yourBtn = {[unowned self] in
      // call your functions here, I hope this will be less laggy 
     print("button pressed")
  }


}