Collection View Cells not appearing

前端 未结 14 825
迷失自我
迷失自我 2021-02-07 08:40

I want to display as many collectionViewCells with buttons as there are strings in my array. but when I start the simulator there is just the backgroun

相关标签:
14条回答
  • 2021-02-07 09:27
    func layoutCells() {
            let layout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
            layout.minimumInteritemSpacing = 5.0
            layout.minimumLineSpacing = 5.0
            layout.itemSize = CGSize(width: (UIScreen.mainScreen().bounds.size.width - 40)/3, height: ((UIScreen.mainScreen().bounds.size.width - 40)/3))
            collectionView!.collectionViewLayout = layout
        }
    

    Try this. Call this function from view did load. I think the problem is that your collection is not laid out correctly.

    func viewDidLoad() {
        layoutCells()
    }
    

    If this works you can modify the layout options to meet your needs.

    0 讨论(0)
  • 2021-02-07 09:28

    I had a similar problem, I found this that was somehow restraining the size of my cell. Hope this helps.

    Select the CollectionViewCell

    Find this values in size inspector

    0 讨论(0)
  • 2021-02-07 09:30

    Ideas:

    • Check if your cell reuse identifier is set up.
    • Check if you've set the collection view subclass correctly in the storyboard.
    • Put print statements inside your cellForItemAtIndexPath function to see if it's being called ever.
    • Not sure if this should be an issue, but Array is actually a type in Swift. Using that as your variable name might be messing with the logic somehow. Rename it to array (lowercase). I believe this is best practice for variable names anyway.
    • Do something else to the cell in cellForItemAtIndexPath, such as changing the background color. If that works, maybe there's just something wrong with what you're doing with tags.
    0 讨论(0)
  • 2021-02-07 09:30

    First of all check you have used this method:-

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return searches.count
    }
    

    There’s one search per section, so the number of sections is the count of the searches array. so also we can use as per needed this method.by default we can use retuen 1

    And Follow step in the Link for better soluation Check this link

    0 讨论(0)
  • 2021-02-07 09:31

    Did you set the CollectionViewController to the storyboard identity inspector? :)

    And I would try to call the reloadData() after you change the data in the viewDidLoad method.

    Hope that helps

    0 讨论(0)
  • 2021-02-07 09:31

    Your problem is in

    func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int)

    method signature. It is wrong. It should be

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

    and with override specification (because UICollectionViewController has own, this why you get empty collection).

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return Array.count
    }
    
    0 讨论(0)
提交回复
热议问题