自定义UICollectionViewLayout:获取某个单元格的大小

Bra*_*uck 3 objective-c ios uicollectionview uicollectionviewcell uicollectionviewlayout

我正在UICollectionViewLayout根据客户的设计规范为客户构建定制,以实现他们想要的外观和流程.我遇到的问题涉及我所定位的单元格的大小.

有可能单元格可能有不同的大小,我想要的布局样式需要特定的间距要求,以便单元格不会相互重叠,这意味着-layoutAttributesForItemAtIndexPath:在布局过载期间,我想查看项目大小,以便可以正确设置和使用它们.

我遇到的问题是,似乎UICollectionView依赖于这种方法来设置单元格的大小,因为在我设置属性之前,我用来从单元格中获取大小的任何方法总是会产生一个CGSizeZero.

这是我要设计的东西吗?Storyboard中单元格的大小是不可访问的(至少我目前所知的方法)所以除了使用特定的宽度/高度(仅适用于一个)之外,我无法解决这个问题.两个可能的单元格)来测试和编写布局代码.

最终:你如何获取UICollectionViewCella UICollectionViewLayout在计算中使用的大小?.

编辑:

我尝试了以下方法:

CGRect frame = [[self.collectionView cellForItemAtIndexPath:path] frame];
CGSize size = [[self.collectionView cellForItemAtIndexPath:path] intrinsicContentSize];
CGRect frame = [[[self.collectionView cellForItemAtIndexPath:path] contextView] frame];
Run Code Online (Sandbox Code Playgroud)

dok*_*ebi 6

我从来没有找到办法避免重复代码中的大小计算.这是我通常做的事情:

获得布局中的大小:

布局可以与其集合视图对话以查找大小(以及诸如最小项目间间隔等).您可以在-prepareLayout或中执行以下操作-layoutAttributesForItemAtIndexPath::

if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:)]) {
    id<UICollectionViewDelegateFlowLayout> delegate = (id<UICollectionViewDelegateFlowLayout>) self.collectionView.delegate;
    size = [delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
}
else {
    size = self.itemSize;
}
Run Code Online (Sandbox Code Playgroud)

现在,您的集合视图委托只需要能够响应collectionView:layout:sizeForItemAtIndexPath:,即使没有自定义布局类也是如此.

首先计算尺寸:

有时提供正确的尺寸有点麻烦.没有神奇的方法来获得单元格的大小而没有实际实例化并放置它,所以我总是自己编写代码.我的大多数集合视图单元都有一个类方法:

+ (CGSize)sizeWithItem:(id<MyDisplayableItem>)item;
Run Code Online (Sandbox Code Playgroud)

当我需要一个单元格所需的大小时,我从我的集合视图委托中调用它:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    Note* note = self.notes[indexPath.item];
    CGSize result = [CSDetailNoteCell sizeWithNote:note];
    // or [MyCell sizeWithItem:myItem andMyOtherItem:somethingElse]
    return result;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,该+sizeWithItem:方法不是免费的,但至少它保持大小计算封装在Cell类中.我倾向于在类中存储一个常量,表示永远不会改变的事物的大小(填充,也许是图像).然后,我将固定大小与需要动态计算的内容相结合,以找到最终大小.动态的东西通常包括对NSString -boundingRectWithSize:options:attributes:context:等的一些调用.

样本量计算:

这个例子使用一个宽度始终为320的单元格,但我认为这足以得到这一点.这是来自的代码CSDetailNoteCell.m.

static CGFloat const CSDetailNoteCellWidth = 320.f;
static CGFloat const CSDetailNoteCellHeightConstant =
        10 + // top padding
        16 + // date label height
        0 + // padding from date label to textview
        8 + // textview top padding
        8 + // textview bottom padding
        22 // bottom padding
;

...

+ (CGFloat)textWidth {
    return CSDetailNoteCellTextViewWidth - 10; // internal textview padding
}

+ (CGSize)sizeWithNote:(CSNote *)note {
    CGFloat textHeight = [self _textHeightWithText:note.text];
    CGSize result = CGSizeMake(CSDetailNoteCellWidth, CSDetailNoteCellHeightConstant + textHeight);
    return result;
}

+ (CGFloat)_textHeightWithText:(NSString*)text {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    CGSize maxSize = CGSizeMake(CSDetailNoteCell.textWidth, CGFLOAT_MAX);
    NSDictionary* attributes = @{NSFontAttributeName: CSDetailNoteCell.font, NSParagraphStyleAttributeName: paragraphStyle};
    CGRect boundingBox = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    CGFloat textHeight = ceilf(boundingBox.size.height);
    return textHeight;
}
Run Code Online (Sandbox Code Playgroud)