0

据我所知,“协议方法”:

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

自动设置我创建的单元格的边界。这对我的 来说很好,而且很花哨UICollectionViewCells,但我需要其中一个在我指定的位置。任何正确方向的指针将不胜感激。

4

1 回答 1

1

我可以假设您还使用一个实例UICollectionViewFlowLayout作为您的集合视图的布局对象吗?

如果是这样,快速而肮脏的答案是子类UICollectionViewFlowLayout化并覆盖该-layoutAttributesForItemAtIndexPath:方法:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.item == 2) // or whatever specific item you're trying to override
    {
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        layoutAttributes.frame = CGRectMake(0,0,100,100); // or whatever...
        return layoutAttributes;
    }
    else
    {
        return [super layoutAttributesForItemAtIndexPath:indexPath];
    }
}

您可能还需要覆盖-layoutAttributesForElementsInRect:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *layoutAttributes = [super layoutAttributesForElementInRect:rect];
    if (CGRectContainsRect(rect, CGRectMake(0, 0, 100, 100))) // replace this CGRectMake with the custom frame of your cell...
    {
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        layoutAttributes.frame = CGRectMake(0,0,100,100); // or whatever...
        return [layoutAttributes arrayByAddingObject:layoutAttributes];
    }
    else
    {
        return layoutAttributes;
    }
}

然后在UICollectionViewFlowLayout创建集合视图时使用新的子类。

于 2013-07-31T00:37:06.627 回答