【发布时间】:2016-12-21 23:07:16
【问题描述】:
我正在尝试在我的 UICollection 视图的标题中添加一个点击手势识别器,但无论如何,我都无法启动 numberOfPostsViewTapped() 函数。我已经尝试了几个小时,并尝试在标题视图中使用其他 UI 元素,例如其他视图或标签,但没有任何帮助。一些指导将不胜感激。
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:))。 -
我确实编译...我用你的行替换并得到错误
ProfileViewController.swift:117:66: Value of type 'ProfileViewController' has no member 'numberOfPostsViewTapped(sender:)'
标签: ios swift uicollectionview uitapgesturerecognizer