Swift 3.0:自定义CollectionView标题按钮单击
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift 3.0:自定义CollectionView标题按钮单击相关的知识,希望对你有一定的参考价值。
我制作了一个自定义的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中使按钮可点击?
答案
第二种方式(不是第二种,我的更好的版本)来自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
}
另一答案
像这样更改您的单元格类:
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")
}
以上是关于Swift 3.0:自定义CollectionView标题按钮单击的主要内容,如果未能解决你的问题,请参考以下文章
自定义幻灯片 Segue - Xcode 8.0 Swift 3.0
想要使用 Swift 3.0 发送自定义电子邮件,即来自 firebase 的欢迎邮件
在 Swift 3.0 中设置 Alamofire 自定义目标文件名而不是使用建议的DownloadDestination
界面生成器中的 iOS Swift 3.0 自定义视图导致重新编译和错位