【问题标题】:Swift 3.0 : Custom CollectionView header button clickSwift 3.0:自定义 CollectionView 标题按钮单击
【发布时间】:2017-11-28 09:06:26
【问题描述】:

我已经制作了一个自定义的 collectionview 单元格。我已经通过这段代码将它作为集合视图的标题:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionElementKindSectionHeader {

        let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderCell", for: indexPath) as! GridHeaderCollectionViewCell
        cell.pagePluginBtn.tag = 0
        cell.tag = 0
        cell.nameLabel.text = pageRecord["GroupName"] as? String
        cell.pagePluginBtn.addTarget(self, action: #selector(TappedOnPagePluginBtn(sender:)), for: .touchUpInside)
        return cell
    }
    abort()
}

func TappedOnPagePluginBtn(sender:UIButton){

    print("in plugin")
}

单元格定义为:

class GridHeaderCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var pagePluginBtn: UIButton!
}

TappedOnPagePluginBtn() 根本没有被调用。有没有办法让collectionView的headerView中的按钮可以点击?

【问题讨论】:

    标签: ios swift3 uibutton uicollectionview uicollectionviewcell


    【解决方案1】:

    像这样改变你的单元格:

    public typealias ButtonAction = () -> Void
    
    class GridHeaderCollectionViewCell: UICollectionViewCell {
    
        @IBOutlet weak var nameLabel: UILabel!
        @IBOutlet weak var pagePluginBtn: UIButton!
    
        var pagePluginButtonAction : ButtonAction?
    
    
        override func viewDidLoad(){
            pagePluginBtn.addTarget(self, action: #selector(pagePluginBtnClick(_:)), for: .touchUpInside)
        }
    
        @objc func pagePluginBtnClick(_ sender : UIButton){
            guard let pagePluginButtonAction = pagePluginButtonAction else{
                return
            }
    
            pagePluginButtonAction()
        }
    }
    

    你需要做的就是:

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerReuseIdentifier, for: indexPath) as! GridHeaderCollectionViewCell
    
        view.pagePluginButtonAction = {
            self.TappedOnPagePluginBtn()
        }
    
        return view
    }
    
    func TappedOnPagePluginBtn(){
    
        print("in plugin")
    }
    

    【讨论】:

      【解决方案2】:

      第二种方法(不是第二种方法,是我的更好版本)来自 ALCameraViewController 的创建者 AlexLittlejohn。

      您可以创建 UIButton 扩展来添加添加目标等操作。

      typealias ButtonAction = () -> Void
      
      extension UIButton {
      
          private struct AssociatedKeys {
              static var ActionKey = "ActionKey"
          }
      
          private class ActionWrapper {
              let action: ButtonAction
              init(action: @escaping ButtonAction) {
                  self.action = action
              }
          }
      
          var action: ButtonAction? {
              set(newValue) {
                  removeTarget(self, action: #selector(performAction), for: .touchUpInside)
                  var wrapper: ActionWrapper? = nil
                  if let newValue = newValue {
                      wrapper = ActionWrapper(action: newValue)
                      addTarget(self, action: #selector(performAction), for: .touchUpInside)
                  }
      
                  objc_setAssociatedObject(self, &AssociatedKeys.ActionKey, wrapper, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
              }
              get {
                  guard let wrapper = objc_getAssociatedObject(self, &AssociatedKeys.ActionKey) as? ActionWrapper else {
                      return nil
                  }
      
                  return wrapper.action
              }
          }
      
          @objc func performAction() {
              guard let action = action else {
                  return
              }
      
              action()
          }
      }
      

      那么;

      override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
      
          let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerReuseIdentifier, for: indexPath) as! GridHeaderCollectionViewCell
      
          view.pagePluginBtn.action = {
              print("yeah we catch it!")
          }
      
          return view
      }
      

      【讨论】:

        猜你喜欢
        • 2020-01-22
        • 2012-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-09
        • 1970-01-01
        • 2021-02-14
        • 1970-01-01
        相关资源
        最近更新 更多