据我所知,“协议方法”:
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
自动设置我创建的单元格的边界。这对我UICollectionViewCells
来说都很好,但是我需要其中一个在我指定的位置。任何指向正确的方向将不胜感激。
答案 0 :(得分:0)
我可以假设您还使用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
。