customization - UICollectionView 单元格起始边距

标签 customization uicollectionview

我正在使用 UICollectionView 来显示彩票号码。 我希望 collectionView 的单元格在自定义间距之后开始,并且在该间距中我想显示标题或某种自定义 UIView。

现在我的 Collection View 如下所示: 123 第456章 第789章

我希望它看起来像这样:

查看12 345678 9.....

我正在考虑使用自定义布局,但 UICollectionViewFlowLayout 可以满足我的目的。

这个问题有解决办法吗?

最佳答案

已解决。

由于我只需要稍微调整一下布局,我决定使用 UICollectionViewFlowLayout 的子类。 这样,我可以按照自己的意愿“玩”布局,更改页眉/页脚等的框架。

我的解决方案是这样的:

.h

@interface MyUICollectionViewFlowLayout : UICollectionViewFlowLayout

@end

.m

#define HEADER_VIEW_WIDTH 132
#define HEADER_VIEW_HEIGHT 44

@interface MyUICollectionViewFlowLayout ()

@end

@implementation MyUICollectionViewFlowLayout

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *allAttributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

    for (UICollectionViewLayoutAttributes *attribute in allAttributes) {

// if it's a cell, change it's layout to be right after the Header, and in the same line.
        if (attribute.representedElementCategory == UICollectionElementCategoryCell) {

            switch (attribute.indexPath.row) {
                case 0:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH, 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 1:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 2:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 2 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 3:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 3 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 4:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 4 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 5:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 5 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
                case 6:
                    attribute.frame = CGRectMake(HEADER_VIEW_WIDTH + attribute.frame.size.width * 6 , 0, attribute.frame.size.width, attribute.frame.size.height);
                    break;
            // if it's a header view, move it to the same line as all the cells.
        } else if (attribute.representedElementCategory == UICollectionElementCategorySupplementaryView){

            attribute.frame = CGRectMake(0, 0 , HEADER_VIEW_WIDTH, HEADER_VIEW_HEIGHT);
        }
    }
    return allAttributes;
}



@end

我想我可以编写一些算法,而不是 switch/case,但我更愿意将优化问题留到以后再处理。

关于customization - UICollectionView 单元格起始边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19474667/

相关文章:

java - 开源 Alfresco

objective-c - 条件导入头文件只有iOS5

linux - 有没有办法调整此通知的大小?

ios - 滚动时应用 CollectionView layerMask "jitter"

ios - 意外发现 nil UIImageView

ios - 更新 UICollectionView 中的 UILabel

ios - CollectionView 内的 CollectionView | CollectionViewCell 自动布局错误

python - 使用 "Restarting & Run All"时未应用 Jupyter notebook custom.js

dialog - 如何使用 WIX 自定义对话框窗口图标?

ios - 在方向上更改 UICollectionViewCell 布局