UICollectionView: different size items is not calculated on reused items

前端 未结 4 1008
北海茫月
北海茫月 2021-02-13 22:27

I have a collection view with varied item sizes which i declare in

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UIColle         


        
相关标签:
4条回答
  • 2021-02-13 22:38

    You need to Override this function in custom layout class. if you are using custom layout.

    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
    return true
    }
    
    0 讨论(0)
  • 2021-02-13 22:40

    It seems that collectionView:layout:sizeForItemAtIndexPath: is called once on layout preparation, before CollectionView is rendered.

    So whenever you scroll and a cell is dequeued it is resized to the size provided by collectionView:layout:sizeForItemAtIndexPath: at preparation stage. Are you expecting this method to be called whenever a cell with certain indexPath gets visible? So that you can dynamically change cell size? seems this is not how this method can be used, it can be used to determine the size of a cell once at layout preparation stage.

    In case you need the sizes to be recomputed for some reason, you can invalidate the layout using [UICollectionViewLayout invalidateLayout].

    0 讨论(0)
  • 2021-02-13 22:45

    A collection view seems to recompute items less often than a UITableView. You have to inform the collection view about most of the changes to the underlying model, e.g. by calling [collectionView reloadData] or [collectionView insertItemsAtIndexPaths:indexPaths].

    0 讨论(0)
  • 2021-02-13 22:51

    You can simple override the scroll view delegate

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        collectionView.collectionViewLayout.invalidateLayout()
    }
    
    0 讨论(0)
提交回复
热议问题