我正在使用 SCLAlertView 创建自定义警报 View 。我的警报 View 包含一个文本字段和彩色单元格的 Collection 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 层次结构的屏幕
感谢您的帮助。
最佳答案
我查看了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/