gpt4 book ai didi

swift - 填充 Collection View 时如何判断是否选择了 Collection View 标题中的按钮?

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:30 24 4
gpt4 key购买 nike

如何判断在填充我的收藏 View 时我是否选择了收藏 View 标题中的按钮?我在标题中有 2 个选项卡,用于确定我用哪些数据填充 Collection View ,因此我希望能够在用户选择不同的选项卡时切换数据并重新加载 Collection View 。

根据要求从头类中获取一些代码...虽然它只是一个按钮,但我看不出有什么意义。我想看看在填充单元格和单元格计数等时是否选择了此按钮。

class UserSearchHeader: UICollectionReusableView {

@IBOutlet weak var friendsButton: UIButton!
@IBOutlet weak var peopleButton: UIButton!
@IBOutlet weak var customSlider: UIView!

override func awakeFromNib() {
super.awakeFromNib()
self.friendsButton.selected = true
customSlider.layer.cornerRadius = customSlider.frame.height / 2

}

@IBAction func friendsButtonTapped(sender: AnyObject) {
if self.friendsButton.selected == false {
self.friendsButton.selected = true
self.peopleButton.selected = false
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.customSlider.frame.origin.x = 0
})
}
}

最佳答案

UICollectionviewHeader + UI 按钮
UIcollectionviewHeader 正在使用 dequeueReusableSupplementaryView 并且出于某种原因它在每个 Headerview 的前面创建了一个 UIView,这将阻止所有正在发生的手势。解决方案是像这样将 View 置于最前面:

override func awakeFromNib() {
super.awakeFromNib()
self.bringSubview(toFront: friendsButton) // this depends on you .xib
}

这将解决您的手势问题。

有多种方法可以在 CollectionViewHeader 中创建 UIButton 操作。

  • AddTarget(由@derdida 回答)
  • CollectionViewHeader 类中的拖放操作。

添加目标:-(引用 derdida 回答)
viewForSupplementaryElementOfKind//ps: 你需要注册你的 CollectionView 类和 Nib

  1. 像这样将 Target 添加到 UIButton。

header.friendsButton.tag = 0
header.friendsButton.addTarget(self, Action :#selector(self.HeaderClick(_:)), for: .touchUpInside)

  1. 像这样创建函数。

@objc func HeaderClick1(_ sender : UIButton){ print("sender.tag\(sender.tag)") }//sender.tag 0

CollectionViewHeader 类中的拖放操作。
对于这个例子,我将使用@Wayne Filkins 问题。

  1. Ctrl + 将 UIButton 拖到标题类
  2. 选择连接类型以操作创建函数,如下所示

@IBAction func friendsButtonTapped(_ sender: Any) { print("something from friendsButtonTapped") }

就是这样。

但这里的主要关键是让 View 在前面。使用调试 View 层次结构了解更多信息。

有人可以修复我的代码 View !

更新解决方案
使用 UIButtonSetOnClickListener.Swift 代码如下:

extension UIControl {
func setOnClickListener(for controlEvents: UIControl.Event = .primaryActionTriggered, action: @escaping () -> ()) {
removeTarget(nil, action: nil, for: .allEvents)
let sleeve = ClosureSleeve(attachTo: self, closure: action)
addTarget(sleeve, action: #selector(ClosureSleeve.invoke), for:
controlEvents)
}
}
class ClosureSleeve {
let closure: () -> ()

init(attachTo: AnyObject, closure: @escaping () -> ()) {
self.closure = closure
objc_setAssociatedObject(attachTo, "[\(arc4random())]", self, .OBJC_ASSOCIATION_RETAIN)
}

@objc func invoke() {
closure()
}
}

这里的使用方法是:
添加目标:-(引用 derdida 回答)
viewForSupplementaryElementOfKind//ps: 你需要注册你的 CollectionView 类和 Nib

    header.friendsButton.setOnClickListener {
//put your code here
}

关于swift - 填充 Collection View 时如何判断是否选择了 Collection View 标题中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39073435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com