GJain
GJain

Reputation: 5093

CollectionView dequeCell with removing subviews from cell makes view appear slow

I have code:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 30;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:REUSE_IDENTIFIER forIndexPath:indexPath];
    [[cell subviews]
     makeObjectsPerformSelector:@selector(removeFromSuperview)];
     [cell addSubview:someView];
    return cell;
}

I am not able to figure out what could be causing the slowness??

Can you provide hints?

Upvotes: 0

Views: 769

Answers (1)

NavinDev
NavinDev

Reputation: 995

Your appearance is slow because you are iterating through all the subviews within the cell and removing from superview. This is a very expensive process and should not be done in the cellForItemAtIndexPath method of the collectionView data source/ Infact this should never be done. If you need to display relevant content you need to access the instance of the cell and update the properties of the UI elements within the cell.

Upvotes: 1

Related Questions