didSelectItemAt 无法从 SCLAlertView 工作

didSelectItemAt not working from SCLAlertView

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

问题是 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。有人知道如何解决这个问题吗?来自视图层次结构的屏幕 感谢您的帮助。

您需要在协议部分添加 collectionview 委托。并确保你有你的对象的出口。

首先你需要像这样更新:

class YourViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
}

然后在viewDidLoad:

collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData() 

并确保您的对象已正确连接。

您还可以为 CollectionView 委托添加扩展:

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

}

如果您在 UICollectionViewCell 中添加了任何 UIImageView 或 UILabel,请确保您已启用 UserIntraction,因为 UIImageView 或 UILabel 默认为 false。

将 UIImageView 或 UILabel 的 UserIntraction 设置为 TRUE。

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

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

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

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