1

I'm trying to set the product options if images into UICollectionView with user interaction enabled so user can select the option he/she prefers before adding the product to the cart.

@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)
                        }

Since it's a subview it should follow the main optionview Y position. Once I test the app and try to load the product page the app crashes.

EDIT: Thanks to @Sh_Khan posting an answer now I get a different error:

The behavior of the UICollectionViewFlowLayout is not defined because: the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values. The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: . Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger. * Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.54.4/UICollectionView.m:5283 * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier catalogimage - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

It sounds we're having conflict with another images parsed from JSON

David Buik
  • 522
  • 1
  • 8
  • 31

1 Answers1

1

You need to use this initalizer

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)

//

You need to register the collectionView with the cell

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

Or this if you have cell with xib

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

Here nib name should be ImagesCollectionViewCell.xib with class ImagesCollectionViewCell

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Sorry might be a silly question where should be init – David Buik Aug 11 '18 at 22:12
  • Thanks for your support I tried this couple of minutes ago [https://stackoverflow.com/a/40417583/9523050] when I found this answer. But now the returning error is different `The behavior of the UICollectionViewFlowLayout is not defined because: he item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.` I'll edit my post for the full error – David Buik Aug 11 '18 at 22:19
  • 1
    look to this https://stackoverflow.com/questions/32082726/the-behavior-of-the-uicollectionviewflowlayout-is-not-defined-because-the-cell – Shehata Gamal Aug 11 '18 at 22:22
  • you need to go through this tutorial https://www.raywenderlich.com/975-uicollectionview-tutorial-getting-started Or this https://www.ioscreator.com/tutorials/collection-view-controller-ios-tutorial-ios-11 – Shehata Gamal Aug 11 '18 at 22:30
  • Thanks for your share I'll check it now, thanks again! – David Buik Aug 11 '18 at 22:32
  • I've checked the tutorial I've successfully created a UICollection in a different ViewController and working fine. However this case a bit different since I'm trying to parse product options in a product page that options can be either checkbox, radio, text, date or image. So I'm defining the image to be set in a UICollection rather a UIView so all options will be in one row and allow the customer to pick his option and add item to cart. i hope this clarify my point – David Buik Aug 11 '18 at 22:34
  • You need to create a UICollectionViewCell subclass and register it , then run and tell if you got any errors preferably create it with xib – Shehata Gamal Aug 11 '18 at 22:39
  • Creating an XIB even if it's a subview and not allows presented? – David Buik Aug 11 '18 at 22:45
  • xib is a layout for the collection , regardless it's in a subview or not – Shehata Gamal Aug 11 '18 at 22:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177921/discussion-between-david-buik-and-sh-khan). – David Buik Aug 13 '18 at 04:07