Ios 单击集合视图单元格时UITapgestureRecognitor应用程序崩溃
我试图在我的集合视图中允许用户交互。我已决定尝试实现UITapGestureRecognizer来实现这一点。我已尝试向collectionview本身和collectionview单元格添加UITapGestureRecognitor。两种方式都会使应用程序崩溃。下面是我如何将UITapgestureRecognitor添加到单元格中的Ios 单击集合视图单元格时UITapgestureRecognitor应用程序崩溃,ios,swift,uigesturerecognizer,Ios,Swift,Uigesturerecognizer,我试图在我的集合视图中允许用户交互。我已决定尝试实现UITapGestureRecognizer来实现这一点。我已尝试向collectionview本身和collectionview单元格添加UITapGestureRecognitor。两种方式都会使应用程序崩溃。下面是我如何将UITapgestureRecognitor添加到单元格中的 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPat
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "userCell", for: indexPath) as! UserCell
cell.userImage.sd_setImage(with: URL(string: self.user[indexPath.row].imagePath))
cell.nameLabel.text = self.user[indexPath.row].username
cell.userID = self.user[indexPath.row].userID
let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "segueToProfile:")
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
cell.addGestureRecognizer(singleTap)
return cell
}
当我点击手机时,AppDelegate中会出现一个SIGABRT。错误消息显示“以NSException类型的未捕获异常终止”。我做错了什么。UITapGestureRecognitor
这是我的segueToProfile函数:
func segueToProfile(gesture: UITapGestureRecognizer) {
// if(recognizer.state == UIGestureRecognizer.State.ended){
// print("myUIImageView has been tapped by the user.")
// }
print("hell world")
}
如果使用
didSelectItemAt
collectionView方法,则代码库看起来可读且可维护
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
currentViewController.performSegue(withIdentifier: "YourSegueName", sender: nil)
}
首先,我将去掉
TapGestureRecognitor
,因为didSelectItem
应该处理您试图完成的任务。这就是说,为了使其发挥作用,您必须:
tappesturerecognizer
.delegate=self
可以在
viewdiload()
处指定上述内容,也可能有堆栈跟踪和更详细的故障消息,其中任何一条都可能有用。你能检查一下吗?我怎样才能得到更详细的故障信息?我是Xcode新手,只能看到调试器控制台中的内容。与在堆栈跟踪中一样,您是否希望在控制台中键入“bt”时显示什么?很奇怪,您使用手势识别器而不是默认的单元格选择方法有什么原因吗?您应该使用didSelectItemAtIndexPath:集合视图委托的方法,而不是自定义的TapSignature。@udbhateja didSelectItemAt()不起作用。My collectionview不响应用户输入。我已经检查了所有的用户交互字段,它们都设置为true,但仍然不起作用。您知道如何使didSelectItemAt()工作吗?didSelectItemAt不工作。“我的收藏”视图不响应用户交互。你知道为什么会发生这种情况吗?也许你还在某个地方使用TapGestureRecognitor,它会阻止选择单元格。检查它们,这应该是问题所在。以防万一,集合视图是否分配了委托
?@GlebA。正在为集合视图委托分配collectionview.delegate=self。还有其他方法吗?@EmreDeğirmenci在我使用手势识别器之前,我正在尝试didSelectItemAt()。didSelectItemAt()根本不起作用。你还有其他的想法吗?