ios - didSelectItemAt 无法从 SCLAlertView 工作

标签 ios swift swift3 uicollectionview uialertview

我正在使用 SCLAlertView 创建自定义警报 View 。我的警报 View 包含一个文本字段和彩色单元格的 Collection View custom alert view

问题是 UICollectionView 的 didSelectItemAt 方法不起作用。我认为问题是因为它就像 subview 。但我无法修复它。 我在 UIViewController 有一个 Collection View ,该方法正在运行。这是我的代码

    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

编辑:

我仍然没有找到如何解决这个问题的答案。我认为问题是 subview 交互,因为在警报显示时调用了委托(delegate)方法 cellForItemAt。有人知道如何解决这个问题吗?来自 View 层次结构的屏幕 view 感谢您的帮助。

最佳答案

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

点击识别器可能与 Collection View 使用的点击识别器冲突。

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

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

关于ios - didSelectItemAt 无法从 SCLAlertView 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43119849/

相关文章:

ios - 未授权,跳过过滤谓词应用

ios - 将字符串数组转换为连接字符串数组

ios - 如何获得合适的、任意宽度的文本笔划?

ios - 为什么从 Activity Monitor 和 Instruments 上看到的内存消耗如此不同。

ios - CallKit - 为阻塞加载的号码或存储的 ID 在哪里?

ios - 错误 : An -observeValueForKeyPath:ofObject:change:context: message was received but not handled

ios - 使用 Alamofire 下载视频,然后在 AV 播放器上播放

ios - 如何正确引用/检索在 AppData 中创建的临时文件以将文件上传到服务器?

iphone - 为 MapKit 转换 "world file"GIS?

swift - 现有 Realm 数据库无法在 swift 3.1 xcode 8 中工作