【问题标题】:Custom collection view layout with Indexpath of length 3索引路径长度为 3 的自定义集合视图布局
【发布时间】:2014-05-06 11:17:20
【问题描述】:

我正在尝试创建自定义布局,每个 LayoutAttributes 的索引路径都需要包含一些附加信息,例如部分、行、列。为了结合这一点,我创建了 NSIndexpath 类别,它返回长度为 3 的 NSIndexpath。 下面是类别的代码sn-p

+(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;   }

我想,我会得到与集合视图数据源方法相同的索引路径,但“collectionView cellForItemAtIndexPath:”方法总是返回长度为 2 的索引路径。

我不明白为什么会这样。谁能解释一下这背后的逻辑以及实现预期行为的可用选项是什么?

【问题讨论】:

  • 据我所知,用于 cellForItemAtIndexPath 的 indexPaths 的长度必须为 2。
  • 请您发布您的类别。否则很难提供帮助。
  • 感谢您的回复。 @SebastianBorggrewe 我已经用类别代码 sn-p 更新了我的问题。请检查一下。

标签: ios objective-c uikit uicollectionview nsindexpath


【解决方案1】:

答案很简单,但可能对你来说并不令人满意。 不能那样做。

indexPath 是由 collectionView 创建的,而不是你自己创建的。由于collectionView没有属性列,所以不能使用。

但是,您可以根据您的布局计算列值。您知道元素的大小并获得部分和行,因此您可以计算列。

希望对您有所帮助。

干杯, 塞巴斯蒂安

【讨论】:

  • 嗨塞巴斯蒂安,感谢您的帮助。目前我正在根据 NSIndexPath 的项目索引计算 rowIndex 和 columnIndex。例如 'rowIndex=indexPath.item/totalColumn' 和 'columnIndex=indexPath.item%totalColumn' 但我只是好奇为什么苹果将 'UICollectionView' 索引路径限制为长度为 2 而在另一个句柄上他们创建类层次结构来创建自定义布局。如果我们可以创建自定义布局,那么索引路径中的问题是什么。
【解决方案2】:

我能够通过稍微不同地使用索引来完成这项工作:

@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

集合视图只使用前两个索引,所以只要我们确保前两个索引始终是唯一的,我们仍然可以使用第三个索引来编码我们需要的信息。在这种情况下,我需要有一个部分一个行/列,所以我使用第二个来存储 (row * numberOfColumns) + 列,第三个来存储 numberOfColumns;这样,当集合视图将索引路径返回给我时,我可以将行和列取回。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    相关资源
    最近更新 更多