UICollectionViewCell Only Updates After Scrolling

2019-05-30 14:42发布

问题:

I've got an issue with my UICollectionView where my cells are always initiated blank/in a default state with no data.

The data only appears in the cells after scrolling them in and out of view.

Thoughts? Thanks.

CODE:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"EquipmentCell";
    APInventoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    if (!cell) cell = [[APInventoryCollectionViewCell alloc] init];

    //set cell data...
    cell.label.text = [arrayOfStrings objectAtIndex:indexPath.row];

    return cell;
}

回答1:

Your problem is that you probably set the arrayOfStrings somewhere in the viewDidLoad method of your view controller, the problem with that is that the collectionview datasource calls are done before that.

What you should do in your viewDidLoad method just call [collectionview reloadData]; an you will be fine