0

I implemented a calendar view with UICollectionView, when scrolling the calendar view very fast, it's not smooth. So I'm thinking whether I can load static content of each cell firstly, and then refresh once specific content has been loaded. So how to delay loading specific contents of each UICollectionViewCell

Specifically, in below function, I'll construct each UICollectionViewCell and return it. Now I just want to construct static contents (such as the date), and delay loading specific contents (such as the background color, if I have an event this day, I'll change the background of this cell), so where should I load specific contents, and how to only refresh showing cell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:UICollectionViewCellIdentifier
                                                                                     forIndexPath:indexPath];

    NSDate *date = [self dateAtIndexPath:indexPath];

    cell.dateLabel.text = [date description];

    // This is the part I want to delay, since it's cost.
    if (dataModel.hasEventAtDate(date)) {
        cell.dateLabel.backgroundColor = [UIColor blue];
    }
    return cell;
}
Yuwen Yan
  • 4,777
  • 10
  • 33
  • 63

1 Answers1

0

You may need have an instance variable to track if cells need to update:

Boolean cellNeedsUpdate = NO

In cellForItemAtIndexPath, check if you need to fully update the cells:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (cellNeedsUpdate) {
        // fully update the cell
    } else {
        // do partial update
    }
}

Track end scrolling of the collectionView, then reload the collectionView:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    cellNeesUpdate = !cellNeedsUpdate;
    [collectionView reloadData];
}
Vito Royeca
  • 657
  • 10
  • 20