Domanda

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 ?

È stato utile?

Soluzione

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

Altri suggerimenti

I was able to get this working by using the indices slightly differently:

@interface NSIndexPath (SGBGridLayout)

+ (NSIndexPath *)indexPathForGridSection:(NSInteger)gridSection gridRow:(NSInteger)gridRow gridColumn:(NSInteger)gridColumn numberOfColumns:(NSUInteger)numberOfColumns;

@property (nonatomic, assign, readonly) NSInteger gridSection;
@property (nonatomic, assign, readonly) NSInteger gridRow;
@property (nonatomic, assign, readonly) NSInteger gridColumn;
@property (nonatomic, assign, readonly) NSInteger itemNumber;
@property (nonatomic, assign, readonly) NSInteger numberOfColumns;

@end

@implementation NSIndexPath (SGBGridLayout)

+ (NSIndexPath *)indexPathForGridSection:(NSInteger)gridSection gridRow:(NSInteger)gridRow gridColumn:(NSInteger)gridColumn numberOfColumns:(NSUInteger)numberOfColumns
{
    NSUInteger itemNumber = (gridRow * numberOfColumns) + gridColumn;
    NSUInteger indexes[] = { gridSection, itemNumber, numberOfColumns };
    return [self indexPathWithIndexes:indexes length:3];
}

- (NSInteger)gridSection
{
    return [self indexAtPosition:0];
}

- (NSInteger)itemNumber
{
    return [self indexAtPosition:1];
}

- (NSInteger)numberOfColumns
{
    return [self indexAtPosition:2];
}

- (NSInteger)gridRow
{
    return self.itemNumber / self.numberOfColumns;
}

- (NSInteger)gridColumn
{
    return self.itemNumber % self.numberOfColumns;
}

@end

The collection view only uses the first two indices, so as long as we make sure the first two indices are always unique we can still use the third one to encode the information we need. In this case, I needed to have a section and a row/column, so I used the second to store (row * numberOfColumns) + column, and the third to store numberOfColumns; that way I can get the row and column back out when the collection view returns the index path to me.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top