UICollectionView 在 Header (Swift) 中添加动作
UICollectionView adding action in Header (Swift)
我有一个 collection 视图和一个用于该 collection 视图的 header。我在我的 header 中添加了一个 UISegmentControl,现在我试图将 selectedSegmentIndex 值传递给主 collectionView,所以我试图在我的 UICollectionViewController 中为该段添加目标,但它没有返回任何东西,这是我的代码
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView = self.collectionView?.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath) as collectionHeaderView
headerView.controlSegment.userInteractionEnabled = true
headerView.controlSegment.addTarget(self, action: "segmentAction:", forControlEvents: UIControlEvents.TouchUpInside)
return headerView
}
func segmentAction(sender: UISegmentedControl) {
println(sender.selectedSegmentIndex)
}
正确的控制事件应该是.ValueChanged
:
headerView.controlSegment.addTarget(self,
action: Selector("segmentAction:"), forControlEvents: .ValueChanged)
我有一个 collection 视图和一个用于该 collection 视图的 header。我在我的 header 中添加了一个 UISegmentControl,现在我试图将 selectedSegmentIndex 值传递给主 collectionView,所以我试图在我的 UICollectionViewController 中为该段添加目标,但它没有返回任何东西,这是我的代码
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView = self.collectionView?.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath) as collectionHeaderView
headerView.controlSegment.userInteractionEnabled = true
headerView.controlSegment.addTarget(self, action: "segmentAction:", forControlEvents: UIControlEvents.TouchUpInside)
return headerView
}
func segmentAction(sender: UISegmentedControl) {
println(sender.selectedSegmentIndex)
}
正确的控制事件应该是.ValueChanged
:
headerView.controlSegment.addTarget(self,
action: Selector("segmentAction:"), forControlEvents: .ValueChanged)