私はUICollectionビューのヘッダーにTap Gesture Recognizerを追加しようとしていますが、何に関係なく、numberOfPostsViewTapped()関数を呼び出すことができません。私は何時間も試してきましたが、ヘッダービューで他のビューやラベルなどの他のUI要素を使用しようとしましたが、何も役立たないものです。いくつかの指針は非常に高く評価されます。UICollectionViewヘッダーのUITapGestureRecognizerが機能しませんか?
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader: // only checking header - no footer on this view
// use an external class for the header UICollectionViewCell in order to set outlets on a non-reusable cell
// if you try to set outlets on a reusable cell, such as a header, it will fail
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! ProfileCollectionViewHeader
// dynamically set user profile information
headerView.usernameTextLabel.text = user?.name
headerView.numberOfPostsTextLabel.text = user?.numberOfPosts != nil ? "\(user!.numberOfPosts!)" : "0"
let numberOfPostsViewSelector : Selector = #selector(self.numberOfPostsViewTapped)
let viewPostsViewGesture = UITapGestureRecognizer(target: self, action: numberOfPostsViewSelector)
viewPostsViewGesture.numberOfTapsRequired = 1
viewPostsViewGesture.delaysTouchesBegan = true
headerView.numberOfPostsTextLabel.userInteractionEnabled = true;
headerView.numberOfPostsTextLabel.addGestureRecognizer(viewPostsViewGesture)
return headerView
default:
assert(false, "Unexpected element kind")
}
}
func numberOfPostsViewTapped(sender: UITapGestureRecognizer){
print("HErE")
}
式「#selector(self.numberOfPostsViewTapped)」もコンパイルされますか?あなたのコードを見ると、正しいセレクタは '#selector(self.numberOfPostsViewTapped(sender :))'でなければなりません。 – crizzis
私はコンパイルします...あなたの行に置き換えられ、エラーが発生しました。 'ProfileViewController.swift:117:66: 'ProfileViewController'タイプの値にメンバー 'numberOfPostsViewTapped(送信者:)'がありません。 ' – oppositemind