我正在使用SCLAlertView创建自定义警报视图。我的警报视图包含一个文本字段和彩色单元格的集合视图
问题是UICollectionView的didSelectItemAt方法不起作用。我认为问题是因为它就像子视图。但我无法解决它。 我在UIViewController上有一个集合视图,该方法正在运行。这是我的代码
<script type="text/javascript">
var selectedBrand=[];
$("input[type='checkbox']").change(function () {
$('#brand input:checked').each(function () {
selectedBrand.push($(this).attr('value'));
alert("Test");
})
sendAjaxRequest();
});
此处有更多屏幕:screens
修改
我仍然没有找到答案如何解决这个问题。我认为问题是子视图交互,因为在alert show上调用了委托方法 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)!")
}
}
。有人知道如何解决这个问题吗?视图层次结构中的屏幕
谢谢你的帮助。
答案 0 :(得分:1)
我调查了SCLAlertView
code。它似乎使用点击识别器来解除键盘。
Tap识别器可能与集合视图使用的点击识别器冲突。
要在SCLAlertView
中禁用识别器,您可以使用外观对象:
let appearance = SCLAlertView.SCLAppearance(
disableTapGesture: true
)
let alertView = SCLAlertView(appearance: appearance)
答案 1 :(得分:0)
您还可以为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的setUserIntraction为TRUE。
答案 2 :(得分:-1)
您需要在协议部分添加collectionview委托。并确保你有对象的出口。
答案 3 :(得分:-1)
首先,您需要更新如下:
class YourViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
}
然后在viewDidLoad中:
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
确保您的对象已正确连接。