Search code examples
iosobjective-cuikituicollectionviewnsindexpath

Custom collection view layout with Indexpath of length 3


I am trying to create custom layout and every LayoutAttributes's index path need to contain some additional information like section, row, column. To incorporate that, I created NSIndexpath category which returns NSIndexpath of length 3. Bellow is the code snippet of category

+(NSIndexPath *)indexPathForColumn:(NSUInteger)column forRow:(NSUInteger)row forSection:(NSUInteger)section {

NSUInteger indexPaths[] = {section, row, column};
NSIndexPath *returnIndexPath = [[NSIndexPath  alloc] initWithIndexes: indexPaths length:3];

return returnIndex;   }

I thought, I will get same index path form collection views datasource method but "collectionView cellForItemAtIndexPath:" method always return me index path of length 2.

I have not understand why it is so. can anybody please explain me the logic behind that and what are the option available to achieve expected behavior ?


Solution

  • The answer is very simple and yet unsatisfactory for you maybe. It can't be done like that.

    The indexPath is created by the collectionView and not by yourself. Since the collectionView has no attribute column, you can't use it.

    However what you can do is calculate the column value based on your layout. You know the size of your elements and you get the section and row, hence you can calculate the column.

    Hope that helps.

    Cheers, Sebastian