How do I tell if I button in the collection view header is selected when populating my collection view? I have 2 tabs in the header which determine which data I populate the collection view with so I want to be able to switch the data and reload the collection view when the user selects a different tab.
在填充集合视图时,如何判断是否在集合视图标题中选中了按钮?我在标题中有2个选项卡,用于确定我填充集合视图的数据,因此我希望能够在用户选择其他选项卡时切换数据并重新加载集合视图。
some code from header class as requested...I don't see the point though it's just a button. I want to see if this button is selected while populating the cells and cell count etc.
来自头类的一些代码请求...虽然它只是一个按钮,但我没有看到这一点。我想在填充单元格和单元格数等时查看是否选中了此按钮。
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
})
}
}
2 个解决方案
#1
1
There are 2 delegate methods who are important to decide which items will be shown. For example:
有两种委托方法对决定显示哪些项目非常重要。例如:
You have 2 different items, they are populated in:
您有2个不同的项目,它们填充在:
let items1 = [Item]()
let items2 = [Item]()
Then you have a variable, that holds which items should be shown:
然后你有一个变量,它保存应该显示的项目:
let items1Shown:Bool = true
Now implement the delegate methods with something like:
现在用以下方法实现委托方法:
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(items1Shown == true) {
return items1.count
} else {
return items2.count
}
}
And
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var item:Item!
if(items1Shown == true) {
item = items1[indexPath.row]
} else {
item = items1[indexPath.row]
}
// format your cell
}
And implement any button function
并实现任何按钮功能
func ChangeItems() {
if(items1Shown == true) {
items1Shown = false
} else {
items1Shown = true
}
// reload your collectionView
self.collectionView.reloadData()
}
Edit:
Why not giving your button a target? (Add this where you dequeue your headerView!)
为什么不给你的按钮一个目标? (在将headerView出列的地方添加!)
headerView.friendsButton.addTarget(self, action: #selector(YourViewControllerWithFunction.cellButtonClick(_:)), forControlEvents: .TouchUpInside)
// for example:
// 例如:
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: headerId, forIndexPath: indexPath)
headerView.friendsButton.addTarget(self, action: #selector(YourViewControllerWithFunction.cellButtonClick(_:)), forControlEvents: .TouchUpInside)
return headerView
}
// Class YourViewControllerWithFunction
//类YourViewControllerWithFunction
func cellButtonClick(button: UIButton) {
// you can do anything with that button now
}
#2
0
UICollectionviewHeader + UI Button
UIcollectionviewHeader are using dequeueReusableSupplementaryView and for some reason it created a UIView Infront of every Headerview, this would block all gesture that are happening. the solution is to bring the view to front like so:
UICollectionviewHeader + UI按钮UIcollectionviewHeader正在使用dequeueReusableSupplementaryView,并且由于某种原因它创建了每个Headerview的UIView Infront,这将阻止正在发生的所有手势。解决方案是将视图置于前面,如下所示:
override func awakeFromNib() {
super.awakeFromNib()
self.bringSubview(toFront: friendsButton) // this depends on you .xib
}
this will solve your gesture issue.
这将解决您的手势问题。
Theres Multiple way to Create a UIButton Action inside a CollectionViewHeader.
Theres在CollectionViewHeader中创建UIButton操作的多种方法。
- AddTarget (as Answered by @derdida)
- Drag and Drop Action In CollectionViewHeader Class.
AddTarget(由@derdida回答)
在CollectionViewHeader类中拖放操作。
Add Target :-(refer to derdida answer)
inside of viewForSupplementaryElementOfKind// ps: you need to register your CollectionView Class & Nib
添加目标:-(参考derdida答案)viewForSupplementaryElementOfKind // ps:你需要注册你的CollectionView类和笔尖
- Add Target to UIButton like so.
像这样添加Target到UIButton。
header.friendsButton.tag = 0
header.friendsButton.addTarget(self, action: #selector(self.HeaderClick(_:)), for: .touchUpInside)
header.friendsButton.tag = 0 header.friendsButton.addTarget(self,action:#selector(self.HeaderClick(_ :)),for:.touchUpInside)
- Created the Function like so.
像这样创建了Function。
@objc func HeaderClick1(_ sender : UIButton){ print("sender.tag \(sender.tag)") }//sender.tag 0
@objc func HeaderClick1(_ sender:UIButton){print(“sender.tag \(sender.tag)”)} // sender.tag 0
Drag and Drop Action In CollectionViewHeader Class.
for this example i will used @Wayne Filkins Question.
在CollectionViewHeader类中拖放操作。对于这个例子,我将使用@Wayne Filkins问题。
- Ctrl + Drag UIButton To Header Class
- Select Connection Type to Action create the function like so
Ctrl +将UIButton拖到标题类
选择Connection Type to Action创建这样的函数
@IBAction func friendsButtonTapped(_ sender: Any) { print("something from friendsButtonTapped") }
@IBAction func friendsButtonTapped(_ sender:Any){print(“来自friendsButtonTapped的东西”)}
and thats it.
就是这样。
but the main key here is to get the view to be to front. used debug view hierarchy to know more.
但这里的主要关键是让视图成为前沿。使用调试视图层次结构了解更多。
Someone can fixed my code view please!
有人可以修复我的代码视图!
#1
1
There are 2 delegate methods who are important to decide which items will be shown. For example:
有两种委托方法对决定显示哪些项目非常重要。例如:
You have 2 different items, they are populated in:
您有2个不同的项目,它们填充在:
let items1 = [Item]()
let items2 = [Item]()
Then you have a variable, that holds which items should be shown:
然后你有一个变量,它保存应该显示的项目:
let items1Shown:Bool = true
Now implement the delegate methods with something like:
现在用以下方法实现委托方法:
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(items1Shown == true) {
return items1.count
} else {
return items2.count
}
}
And
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var item:Item!
if(items1Shown == true) {
item = items1[indexPath.row]
} else {
item = items1[indexPath.row]
}
// format your cell
}
And implement any button function
并实现任何按钮功能
func ChangeItems() {
if(items1Shown == true) {
items1Shown = false
} else {
items1Shown = true
}
// reload your collectionView
self.collectionView.reloadData()
}
Edit:
Why not giving your button a target? (Add this where you dequeue your headerView!)
为什么不给你的按钮一个目标? (在将headerView出列的地方添加!)
headerView.friendsButton.addTarget(self, action: #selector(YourViewControllerWithFunction.cellButtonClick(_:)), forControlEvents: .TouchUpInside)
// for example:
// 例如:
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: headerId, forIndexPath: indexPath)
headerView.friendsButton.addTarget(self, action: #selector(YourViewControllerWithFunction.cellButtonClick(_:)), forControlEvents: .TouchUpInside)
return headerView
}
// Class YourViewControllerWithFunction
//类YourViewControllerWithFunction
func cellButtonClick(button: UIButton) {
// you can do anything with that button now
}
#2
0
UICollectionviewHeader + UI Button
UIcollectionviewHeader are using dequeueReusableSupplementaryView and for some reason it created a UIView Infront of every Headerview, this would block all gesture that are happening. the solution is to bring the view to front like so:
UICollectionviewHeader + UI按钮UIcollectionviewHeader正在使用dequeueReusableSupplementaryView,并且由于某种原因它创建了每个Headerview的UIView Infront,这将阻止正在发生的所有手势。解决方案是将视图置于前面,如下所示:
override func awakeFromNib() {
super.awakeFromNib()
self.bringSubview(toFront: friendsButton) // this depends on you .xib
}
this will solve your gesture issue.
这将解决您的手势问题。
Theres Multiple way to Create a UIButton Action inside a CollectionViewHeader.
Theres在CollectionViewHeader中创建UIButton操作的多种方法。
- AddTarget (as Answered by @derdida)
- Drag and Drop Action In CollectionViewHeader Class.
AddTarget(由@derdida回答)
在CollectionViewHeader类中拖放操作。
Add Target :-(refer to derdida answer)
inside of viewForSupplementaryElementOfKind// ps: you need to register your CollectionView Class & Nib
添加目标:-(参考derdida答案)viewForSupplementaryElementOfKind // ps:你需要注册你的CollectionView类和笔尖
- Add Target to UIButton like so.
像这样添加Target到UIButton。
header.friendsButton.tag = 0
header.friendsButton.addTarget(self, action: #selector(self.HeaderClick(_:)), for: .touchUpInside)
header.friendsButton.tag = 0 header.friendsButton.addTarget(self,action:#selector(self.HeaderClick(_ :)),for:.touchUpInside)
- Created the Function like so.
像这样创建了Function。
@objc func HeaderClick1(_ sender : UIButton){ print("sender.tag \(sender.tag)") }//sender.tag 0
@objc func HeaderClick1(_ sender:UIButton){print(“sender.tag \(sender.tag)”)} // sender.tag 0
Drag and Drop Action In CollectionViewHeader Class.
for this example i will used @Wayne Filkins Question.
在CollectionViewHeader类中拖放操作。对于这个例子,我将使用@Wayne Filkins问题。
- Ctrl + Drag UIButton To Header Class
- Select Connection Type to Action create the function like so
Ctrl +将UIButton拖到标题类
选择Connection Type to Action创建这样的函数
@IBAction func friendsButtonTapped(_ sender: Any) { print("something from friendsButtonTapped") }
@IBAction func friendsButtonTapped(_ sender:Any){print(“来自friendsButtonTapped的东西”)}
and thats it.
就是这样。
but the main key here is to get the view to be to front. used debug view hierarchy to know more.
但这里的主要关键是让视图成为前沿。使用调试视图层次结构了解更多。
Someone can fixed my code view please!
有人可以修复我的代码视图!