Swift 3.0:自定义CollectionView标题按钮单击

时间:2017-06-25 07:33:56

标签: ios swift3 uibutton uicollectionview uicollectionviewcell

我制作了一个自定义的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中点击按钮?

2 个答案:

答案 0 :(得分:1)

第二种方式(不是第二种,我的更好的版本)来自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
}

答案 1 :(得分: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")
}