layoutAttributesForSupplementaryViewOfKind:atIndex

2019-06-18 14:57发布

I have a customUICollectionViewLayout that uses supplementary views to place a footer at the bottom of each page.

This seems to have been working well until I began to insert cells at the bottom of a page.

As the supplementary views have to link to an NSIndexPath within the UICollectionView, in my prepare layout method I detect the last cell on the page and the layoutAttributes for the supplementary view are then stored to the last indexPath on the page.

This is the collection view layout prior to inserting a new cell:

    |- Page 1
    |-- [0, 0]
    |-- [0, 1]
    |-- [0, 2]
    |- Footer (indexPath [0, 2])
    |- Page 2
    |-- [1, 0]
    |-- [1, 1]
    |- Footer (indexPath [1, 1])

As expected, my prepareLayout method creates and stores UICollectionViewLayoutAttributes for indexPaths [0, 2] and [1, 1].
layoutAttributesForSupplementaryViewOfKind:atIndexPath: method is also called twice returning the correct attributes for indexPaths ([0, 2] and [1, 1])

The issue now is that when I insert a new cell at the index [1, 2] the prepareLayout method correctly re-calculates the layouts and changes the indexPath of the second footer to [1, 2] like you would expect.

However, when layoutAttributesForSupplementaryViewOfKind:atIndexPath: is called again to update the interface its indexPath property still returns [1, 1]. This then returns nil on the method as it does not exist and causes a crash (obviously)!

Why does layoutAttributesForSupplementaryViewOfKind:atIndexPath: still return the incorrect (old) indexPath? I've checked that the layout is being invalidated and that my footerAttributes dictionary that is storing the layoutAttributes has the correct (new) indexPath so from what I have read this should just work?

It's also important to note that this only occurs when an animated insert takes place... Just calling reloadData doesn't crash.

Also, I can't link the supplementary view to a single section as my custom layout is designed to page the collectionView and sections can overflow onto different pages meaning multiple supplementary views for each section.

Here is a gist of my layout's .m file: https://gist.github.com/liamnichols/6621694

or you can find the repo here: https://github.com/liamnichols/LNCollectionViewPagedLayout/tree/master/Source

My alternate solution would be to assign the supplementary views to a custom NSIndexPath where the indexPath.row value would determine what page the supplementary view is on however I am not sure if this is a suitable way to handle this so thought that I would ask for advice before hand.

Any help would be great,

Thanks.

0条回答
登录 后发表回答