单击收藏夹视图单元格时UITapGestureRecognizer崩溃的应用程序

问题描述 投票:0回答:2

我正在尝试在我的收藏夹视图中允许用户交互。我决定尝试实现UITapGestureRecognizer来执行此操作。我尝试将UITapGestureRecognizer添加到collectionview本身以及collectionview单元中。两种方法都会使应用程序崩溃。这是我将UITapGestureRecognizer添加到单元格的方法。


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的未捕获异常终止”。我究竟做错了什么。 UITapGestureRecognizer。

这是我的segueToProfile函数:


    func segueToProfile(gesture: UITapGestureRecognizer) {
//        if(recognizer.state == UIGestureRecognizer.State.ended){
//            print("myUIImageView has been tapped by the user.")
//        }
        print("hell world")

    }

ios swift uigesturerecognizer
2个回答
0
投票

如果您使用collection的didSelectItemAt方法查看您的代码库,则看起来可读且可维护。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
currentViewController.performSegue(withIdentifier: "YourSegueName", sender: nil)
}

0
投票

首先,我会摆脱tapgesturerecognizer,因为didSelectItem应该可以处理您要完成的工作。话虽如此,为了使它起作用,您必须:

  1. 删除tapgesturerecognizer
  2. 确保将集合视图委托设置为self。

例如<yourColletionViewName>.delegate = self

以上可以在viewDidLoad()处分配

© www.soinside.com 2019 - 2024. All rights reserved.