I am setting up a UICollectionView with a custom UICollectionViewCell Class.
Using the functions specified in the UICollectionViewDelegate I have been able to get a label populated with text in each cell.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "keypadButton", for: indexPath) as! KeypadButtonCollectionViewCell
cell.awakeFromNib()
return cell
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let button = cell as! KeypadButtonCollectionViewCell
//Assigning like this works
button.buttonLabel.text = tempScale[indexPath.row]
}
However;
I initially had it set up differently with a buttonText
class variable in the KeypadButtonCollectionViewCell
Class (as below) and setting that variable in the willDisplay
function (also below)
class KeypadButtonCollectionViewCell: UICollectionViewCell {
var buttonLabel: UILabel!
var buttonText: String!
override func awakeFromNib() {
buttonLabel = UILabel.init(frame: contentView.frame)
buttonLabel.font = UIFont.init(name: "HelveticaNeue-Ultralight", size: 20)
buttonLabel.textColor = UIColor.black
buttonLabel.textAlignment = NSTextAlignment.center
buttonLabel.text = buttonText //Assigning here didn't work
contentView.layer.borderColor = UIColor.init(red: 37/255, green: 37/255, blue: 37/255, alpha: 1).cgColor
contentView.layer.borderWidth = 4
contentView.addSubview(buttonLabel)
}
}
//---------In the view controller--------
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let button = cell as! KeypadButtonCollectionViewCell
//Setting class var string here to be set as label.text not working
button.labelText = tempScale[indexPath.row]
}
What is it I am misunderstanding here? Why did it not like setting the using the assigned class variable to set the label text in the wakeFromNib() method but worked when I set the label text directly?
As mentioned at the beginning, I have a working way of doing this, I am interested in this for academia and to better understand OOP programming.