【问题标题】:UICollectionView must be initialized with a non-nil layout parameter as SubviewUICollectionView 必须使用非零布局参数初始化为子视图
【发布时间】:2019-01-19 02:34:42
【问题描述】:

如果图像进入 UICollectionView 并启用用户交互,我正在尝试设置产品选项,以便用户可以在将产品添加到购物车之前选择他/她喜欢的选项。

@IBOutlet weak var optionView: UIView!
optionView.translatesAutoresizingMaskIntoConstraints = false                 
var Y:CGFloat = -10

        else if dict["type"].stringValue == "image" {
                        let imageOptionView = UICollectionView(frame: CGRect(x: 5, y: Y, width: self.optionView.frame.size.width - 10, height: 30))
                        imageOptionView.isUserInteractionEnabled = true
                        self.optionView.addSubview(imageOptionView)
                        imageOptionView.tag = 11000;
                        Y += 60;
                        let productOptionArray : JSON = JSON(dict["product_option_value"].arrayObject!)
                        let imagesArray:NSMutableArray = []
                        var internalY:CGFloat = 0

                        for j in 0..<productOptionArray.count {
                            let img = UIImageView(frame: CGRect(x: 5, y: internalY, width: imageOptionView.frame.size.width, height: 30))

                            let images = productOptionArray[j]["image"].stringValue
                            let data = NSData(contentsOf: NSURL(string: images)! as URL)
                            img.image = UIImage(data: data! as Data)

                            img.tag = j;
                            img.isUserInteractionEnabled = true
                            imageOptionView.addSubview(img)
                            internalY += 60;
                            imagesArray.add(img)

                            print ("Printing Images", images)
                            print ("Printing ImageView", imageOptionView)
                            print ("Printing One Image", img.image)
                            print ("Printing ImageArray", imagesArray)
                        }

因为它是一个子视图,它应该跟随主选项视图的 Y 位置。一旦我测试应用并尝试加载产品页面,应用就会崩溃。

编辑:感谢@Sh_Khan 发布答案,现在我得到一个不同的错误:

未定义 UICollectionViewFlowLayout 的行为,因为: 项目宽度必须小于 UICollectionView 的宽度 减去部分插入左右值,减去内容 插入左值和右值。相关的 UICollectionViewFlowLayout 实例是,它是 附在 ;层 = ;内容偏移:{0, 0};内容大小:{0, 0};调整内容插入:{0, 0, 0, 0}> 集合视图布局:。 在 UICollectionViewFlowLayoutBreakForInvalidSizes 在 调试器。 * -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] 中的断言失败,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.54.4/UICollectionView.m:5283 * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使视图出列 种类:带有标识符 catalogimage 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类或连接一个 故事板中的原型单元格

听起来我们与从 JSON 解析的其他图像发生冲突

【问题讨论】:

    标签: ios swift swift3 uicollectionview uiimageview


    【解决方案1】:

    你需要使用这个初始化器

    init(frame: CGRect,collectionViewLayout layout: UICollectionViewLayout)
    

    //

    let layout = UICollectionViewFlowLayout()
    layout.itemSize = CGSize(width: 60, height: 30)
    let imageOptionView = UICollectionView(frame: CGRect(x: 5, y: Y, width: self.optionView.frame.size.width - 10, height: 30),layout:layout)
    

    //

    你需要用cell注册collectionView

    imageOptionView.register(ImagesCollectionViewCell.self, forCellWithReuseIdentifier: "catalogimage")
    

    如果你有带有 xib 的单元格,或者这个

     imageOptionView.register(UINib.init(nibName: "ImagesCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "catalogimage")
    

    这里的 nib 名称应该是 ImagesCollectionViewCell.xibImagesCollectionViewCell

    【讨论】:

    猜你喜欢
    • 2014-08-08
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    相关资源
    最近更新 更多