2017-06-25 43 views
0

我做了一个自定义collectionview单元格。我已经把它作为集合视图的头部通过该代码:Swift 3.0:自定义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中可点击?

回答

0

更改单元格类是这样的:

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") 
} 
0

的第二个方法(不是第二,一个更好的版本矿)是从AlexLittlejohn, ALCameraViewController的创建者。

您可以创建一个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 
}