Ios 子类化UICollectionViewLayout并指定给UICollectionView

Ios 子类化UICollectionViewLayout并指定给UICollectionView,ios,uicollectionview,Ios,Uicollectionview,我有一个CollectionViewController - (void)viewDidLoad { [super viewDidLoad]; // assign layout (subclassed below) self.collectionView.collectionViewLayout = [[CustomCollectionLayout alloc] init]; } // data source is working, here's what matt

我有一个CollectionViewController

- (void)viewDidLoad 
{
   [super viewDidLoad];    
   // assign layout (subclassed below)
   self.collectionView.collectionViewLayout = [[CustomCollectionLayout alloc] init];
}

// data source is working, here's what matters:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   ThumbCell *cell = (ThumbCell *)[cv dequeueReusableCellWithReuseIdentifier:@"ThumbCell" forIndexPath:indexPath];

   return cell;
}
我还有一个UICollectionViewLayout子类:CustomCollectionLayout.m

#pragma mark - Overriden methods
- (CGSize)collectionViewContentSize
{
    return CGSizeMake(320, 480);
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return [[super layoutAttributesForElementsInRect:rect] mutableCopy];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // configure CellAttributes
    cellAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];

    // random position
    int xRand = arc4random() % 320;
    int yRand = arc4random() % 460;
    cellAttributes.frame = CGRectMake(xRand, yRand, 150, 170);

    return cellAttributes;
}
我试着给细胞用一个新的框架,只是在一个随机的位置用一个。 问题是未调用layoutAttributesForItemAtIndexPath


提前感谢您的建议。

是故事板参数的问题。从Inspector面板分配自定义UICollectionLayout。

编辑:我没有注意到您已经解决了您的案例,但我遇到了相同的问题,以下是对我的案例有帮助的内容。我把它留在这里,它可能对一些人有用,因为这个话题还没有太多

绘制时,
UICollectionView
不会调用方法
layoutAttributesForItemAtIndexPath:
,而是
LayoutAttributesForElementsRect:
来确定哪些单元格应该可见。您有责任实现此方法,并从中调用所有可见索引路径的
layouttributesforItemAtIndexPath:
,以确定确切位置

换句话说,将以下内容添加到布局中:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray * array = [NSMutableArray arrayWithCapacity:16];
    // Determine visible index paths
    for(???) {
        [array addObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:??? inSection:???]]];
    }
    return [array copy];
}

有人知道在哪里可以找到UICollectionViewLayout的自定义实现示例吗?您可以在非常有趣的网站上查看我的Pinterest灵感的UICollectionViewLayout。。。谢谢分享!我复制你的代码并运行它。正如您所说,
layouttributesforItemAtIndexPath
未被调用。但是在改变故事板中的布局类之后,问题仍然存在。你确定你在这里发布的代码和你实际使用的代码是一样的吗?我从来没有说过上面发布的代码是有效的代码。这只是我如何将CustomLayout分配给UICollectionView的一个要点。对于一个好的工作代码,我建议你看看杰伊·斯卢佩斯基发布的链接。。。这对我帮助很大。你接受了自己的答案,但没有修正问题中的代码。这看起来很有误导性。问题在于如何执行任务,而不是如何构建iOS应用程序的工作部件。您可能误解了。如果
UICollectionViewLayout
中的绘图代码没有调用
layoutAttributesForItemAtIndexPath:
,那么为什么它在类的接口中?!传统?旧的API?计划API?建议一个好的模式?好问题<代码>返回[数组复制]是内存泄漏(它会创建一个非自动释放的新实例),除非您启用了ARC。不管ARC如何,它都是完全不必要的,只会浪费内存和CPU时间。只需返回
array
,这是一种自动释放,可以使用可变数组来代替不可变数组(只要它在返回后不会发生变异),这是我在发布答案时读到的大多数源代码中使用的模式。我的理解是,它实际上是用来节省内存的,因为
NSArray
不需要
NSMutableArray
的所有增强的内部结构。对于现代移动设备来说,CPU被认为是便宜的,而内存则不是,因此强烈建议执行一个小的额外计算,如果它允许您在之后释放一些内存结构。而且,复制一个相当大的数组比重新绘制布局要便宜得多,因此用于此任务的CPU是无关的。当然,使用ARC是假定的。除非有很好的理由(我想不出任何这样的例子),否则通常不会在应用程序中禁用它。