cellAttrsDictionary it is impractical to store a UICollectionViewLayoutAttributes . It is indexed by NSIndexPath , but in layoutAttributesForElementsInRect you are looking for an area. If you need cellAttrsDictionary for something else, you can leave it, but save each cellAttribute element additionally in a different data structure, which is searched faster.
For example, a nested array:
var allCellAttributes = [[UICollectionViewLayoutAttributes]]()
An array of the first level describes the area, for example, in pieces of 1000 pixels high and the width of the screen. Thus, all cellAttributes that intersect with the rectangle {0, 0, screen width, 1000} are included in:
allCellAttributes[0].append(cellAttributes)
All cellAttributes attributes that intersect with the rectangle {0, 1000, screen width, 2000} are included in:
allCellAttributes[1].append(cellAttributes)
And so on...
Then in layoutAttributesForElementsInRect you can search in this data structure by jumping directly into the array depending on the given CGRect. If the line is, for example, direct. {0, 5500, 100, 6700}, you just need to search in this range:
allCellAttributes[5] allCellAttributes[6]
This should give you the basic idea, I hope you understand what I mean.