我正在使用SCLAlertView创建自定义警报视图。我的警报视图包含一个文本字段和彩色单元格的集合视图
ios - didSelectItemAt在SCLAlertView中不起作用-LMLPHP

问题是UICollectionView的didSelectItemAt方法不起作用。我认为问题是因为它就像子视图。但我无法解决。
我在UIViewController上有一个集合视图,并且该方法有效。这是我的代码

    var collectionViewAlert: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
        layout.itemSize = CGSize(width: 25, height: 25)

        collectionViewAlert = UICollectionView(frame: CGRect(x: 18, y: 10, width: 250, height: 25), collectionViewLayout: layout)
        collectionViewAlert.dataSource = self
        collectionViewAlert.delegate = self
        collectionViewAlert.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollCell")
        collectionViewAlert.backgroundColor = UIColor.white

    }

    @IBAction func addCategory(_ sender: Any) {
        let alertView = SCLAlertView()
        alertView.addTextField("Enter category name")

        let subview = UIView(frame: CGRect(x:0,y:0,width:216,height:70))
        subview.addSubview(self.collectionViewAlert)
        alertView.customSubview = subview
        alertView.showEdit("Choose color", subTitle: "This alert view has buttons")



    }



    let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
    var colors = [UIColor.red, UIColor.yellow, UIColor.green, UIColor.blue, UIColor.cyan]

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.colors.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        // get a reference to our storyboard cell
        if (collectionView == self.collectionViewAlert) {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollCell", for: indexPath as IndexPath)
            cell.backgroundColor = self.colors[indexPath.item]
            return cell
        }
        else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath)

            cell.backgroundColor = self.colors[indexPath.item]// make cell more visible in our example project
            return cell
        }

    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.item)!")
    }

}

此处有更多屏幕:screens

编辑:

我仍然找不到答案如何解决这个问题。我认为问题在于子视图交互,因为在警报显示中调用了委托方法cellForItemAt。有人知道如何解决吗?视图层次结构ios - didSelectItemAt在SCLAlertView中不起作用-LMLPHP中的屏幕
谢谢你的帮助。

最佳答案

我研究了SCLAlertView code。似乎它使用了敲击识别器来关闭键盘。

点击识别器可能与集合视图使用的点击识别器冲突。

要禁用SCLAlertView中的识别器,可以使用外观对象:

let appearance = SCLAlertView.SCLAppearance(
    disableTapGesture: true
)
let alertView = SCLAlertView(appearance: appearance)

10-05 20:05