-1

I try to do the collection view as this url How to make a simple collection view with Swift

But the label can't connect to collectionviewcell.swift:

the label can't connect to collectionviewcell.swift

There is a Mycollectionview.swift:

there is a Mycollectionview.swift

Only can show the background but the text not print:

only can show the background but but the text not print

MyControllectionCell.swift

import UIKit
class MyCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var myLabel: UILabel!
}

ViewController.swift

import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
    var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"]
    
    
    // MARK: - UICollectionViewDataSource protocol
    
    // tell the collection view how many cells to make
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count
    }
    
    // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
        
        // Use the outlet in our custom class to get a reference to the UILabel in the cell
        cell.myLabel?.text = self.items[indexPath.item] // The row value is the same as the index of the desired text within the array.
        cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
        
        return cell
    }
    
    // MARK: - UICollectionViewDelegate protocol
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // handle tap events
        print("You selected cell #\(indexPath.item)!")
    }
}
gcharita
  • 6,251
  • 3
  • 16
  • 29
anonymous
  • 39
  • 5

1 Answers1

0

tldr; Check a tutorial first.

Long answer:

I guess you're trying to connect the UILabel to your UIViewController subclass instead of the UICollectionViewCell.

But I'm a bit unsure about it because the code snippet you've provided is not complete... eg. I don't see your UICollectionView property listed under your UIViewController subclass.

So it's just my assumption.

Also unsure about the delegate and dataSource properties are properly set for your UICollecitonView.

Double-check that the label is not nil in cellForItemAt method, and also that it is part of the cell view's hierarchy with a non-zero frame.

But honestly, you should definitely check out the tutorials first. There are so many great tutorials out there that can help you in implementing this step-by-step.

choofie
  • 1,677
  • 1
  • 17
  • 24