Jnguyen22
Jnguyen22

Reputation: 1157

Why is my UICollectionViewCell not getting data from UICollectionViewController?

I have a UICollectionViewController called SwipingController that creates a TeamCell and gives it a teamName of "Boston Celtics"

class SwipingController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = .white
        collectionView?.register(TeamCell.self, forCellWithReuseIdentifier: "cellId")

        collectionView?.isPagingEnabled = true
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell

        cell.teamName = "Boston Celtics"

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height)
    }
}

However, when I run the code, teamName still prints out as an empty string.

class TeamCell: UICollectionViewCell {

    var teamName: String =  ""

    override init(frame: CGRect) {
        super.init(frame: frame)

        print(teamName) //this prints nothing
    }
}

Upvotes: 1

Views: 103

Answers (5)

Ankur Lahiry
Ankur Lahiry

Reputation: 2315

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell

it inits the cell, and so initially it is empty.

After initialization cell.teamName = "Boston Celtics" is called, actually having no effect

you have to update the name

class TeamCell: UICollectionViewCell {

    var teamName: String =  ""

    override init(frame: CGRect) {
        super.init(frame: frame)

        print(teamName) //this prints nothing
    }

    func updateName(name : String) {
         self.testName = name

    }

}

in collectionview

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell

        cell.updateName(name : "Boston Celtics") 

        return cell
 }

Upvotes: 3

Tushar Moradiya
Tushar Moradiya

Reputation: 2118

Please try this :

class TeamCell: UICollectionViewCell {

    var teamName : String = "" {
       didSet {
           print(teamName)
       }
    }

    override func awakeFromNib() {
       super.awakeFromNib()
       //custom logic goes here

    }
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell

    cell.teamName = "Boston Celtics"

    return cell
}

OR

class TeamCell: UICollectionViewCell {


    override func awakeFromNib() {
       super.awakeFromNib()
       //custom logic goes here

    }

    func getTeamName(teamName : String)
    {
        print(teamName)
    }
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell

    cell.getTeamName(teamName: "Boston Celtics")

    return cell
}

Upvotes: 2

Rob
Rob

Reputation: 437392

You are simply looking at teamName too early in the process. By the time cellForItemAt hits the cell.teamName = ... line, the cell’s init method has already been called. So, teamName will always be blank during the init method. But, teamName will be set before the cell appears within the view.


Often cells would just have UIKit controls, such as a UILabel, and you’d set the text of that, e.g.

class TeamCell: UICollectionViewCell {
    var teamLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        configure()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configure() {
        addSubview(teamLabel)

        NSLayoutConstraint.activate([
            teamLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
            teamLabel.centerYAnchor.constraint(equalTo: centerYAnchor)
        ])
    }

    func update(for team: String) {
        teamLabel.text = team
    }
}

And then

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell

    cell.update(for: "Boston Celtics")

    return cell
}

Upvotes: 2

matt
matt

Reputation: 534893

It’s just a matter of order of events. The cell is created, you print, the cell is used in the table view, the team name is assigned.

Upvotes: 0

Shivam Parmar
Shivam Parmar

Reputation: 1570

in TeamCell class try this

var teamcell = String()

override init(frame: CGRect) {
    super.init(frame: frame)

    if teamcell != nil {
        print(teamName)
    }
}

Upvotes: 0

Related Questions