Error Setting Text in Collection View Cell

text for UILabel inside cell not fully displaying

Problem solved. All I needed to do was change UILabel to UITextView.

Found nil while setting value of a label inside Collection view cell

Just minor mistake is there I think, In cellForItemAt indexPath Please update following line,

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

I just paste your code and got following output, nothing else,

Sample Image

If you still unable to produce desired output, just delete existing UILabel from SelectionCollectionViewCell and add it again.

FYI. No need to register cell in viewDidLoad() method

Setting label text in a UICollectionViewCell not working when string is passed to custom class

awakeFromNib gets called automatically after the view and its subviews were allocated and initialized, so don't called it explicitly. So remove the line cell.awakeFromNib().

In your case when awakeFromNib is called your buttonText String is nil and when willDisplay cell method call where you are setting String value buttonText but awakeFromNib is already called before that so setting buttonText there will not update the value of your label.

From the documentation on awakeFromNib:

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.

The way you are doing with your first approach is perfect by setting array element directly to the label's text but if you want to set the String value buttonText then you can go for the observing property didSet of your buttonText like this way.

var buttonText: String = "" {
didSet {
buttonLabel.text = buttonText
}
}

Now when you set buttonText value in willDisplay cell it will update the value of cell's label because of didSet of your buttonText.

custom CollectionViewCell in swift fatal error

Your problem is you created a .xib, but you're registering a class:

let nib = UINib(nibName: view, bundle: nil)
collectionViewCarType.registerNib(nib, forCellWithReuseIdentifier: "MainScreenCollectionViewCell")

This means that when it loads your class, it's just initialized, it doesn't load it from the .xib, so your outlets are never set.

Changing the title and the description below the collectionView cell When the collection view cell change. Doing this with index path is not working

You seem to be using a common titleLabel and descriptionLabel and setting the title and description in cellForItemAt, which is called when the cell is loaded. Move the following from IntroPage to your cell CollectionViewCellIntroScene (modify the xib by adding the title and desc labels):

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!

and in cellForRowAt:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCellIntroScene
cell.image.image = collectionViewImages[indexPath.item]
cell.titleLabel.text = collectionViewTitleTexts[indexPath.item]
return cell
}

swift - UICollectionView issue fatal error: unexpectedly found nil while unwrapping an Optional value

Put some breakpoints in your code. Step through each of your functions and look at the values contained by the variables. Find the variable that is nil when it isn't supposed to be.

If the error actually is in the code you presented, then the error is likely in the cell.askingUsernameLabel.text = arr[indexPath.row] line or the self.collectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier:"CollectionViewCell") line.

Do you know how to put in an exception breakpoint? Here's how:

Sample Image



Related Topics



Leave a reply



Submit