0

我试图UICollectionView通过以编程方式构建它来教自己如何使用它。我按照一些教程创建了一个大小为 500 x 500 的示例集合视图,每个单元格为 245 x 45。到目前为止,我能够看到集合视图,但它不包含任何单元格,我不知道为什么。这是我所做的:

//子类 UICollectionViewLayout

@interface LabelCVLayout : UICollectionViewLayout

@property (nonatomic) UIEdgeInsets itemInsets;
@property (nonatomic) CGSize itemSize;
@property (nonatomic) CGFloat interItemSpacingY;
@property (nonatomic) NSInteger numberOfColumns;

@end

@implementation LabelCVLayout

- (id)init 
{
    self = [super init];
    if (self) {
        [self setup];
    }

    return self;
}

- (void)setup
{
    self.itemInsets = UIEdgeInsetsMake(22.0f, 22.0f, 13.0f, 22.0f);
    self.itemSize = CGSizeMake(245.0f, 45.0f);
    self.interItemSpacingY = 12.0f;
    self.numberOfColumns = 3;
}

@end

//显示集合视图的视图控制器

@interface TempViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (strong, nonatomic) UICollectionView *collectionView;

@end

@implementation TempViewController


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1; 
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
 numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

//THE BELOW DELEGATE METHOD DOES NOT GET CALLED!
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView   cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newCell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor whiteColor];

    UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.size.width, cell.size.height)];
    testLabel.font = [UIFont fontWithName:ProximaNovaSemibold size:15];
    testLabel.text = [NSString stringWithFormat:@"Label %i", indexPath.row];
    testLabel.clipsToBounds = YES;
    testLabel.backgroundColor = [UIColor clearColor];
    testLabel.textColor = [UIColor blackColor];
    testLabel.textAlignment = NSTextAlignmentLeft;

    [cell addSubview:testLabel];

    NSLog(@"test!"); //does not print!

    return cell;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    LabelCVLayout *flowLayout = [[LabelCVLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0,500,500) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor redColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"newCell"];

    [self.view addSubview:self.collectionView];
}

@end

奇怪的是,在设置断点后,我发现numberOfSectionsnumberOfItemsInSection数据源方法被调用,但该cellForItemAtIndexPath方法没有被调用。

我想知道是否1) 任何人都可以看到我错过了什么?2)如果我以后想创建一个集合视图,根据单元格大小和集合视图大小的大小约束将尽可能多的单元格添加到一行中(即我不想定义一个固定的数字列),我是否必须继承 UICollectionViewFlowLayout 而不是 UICollectionViewLayout?

4

1 回答 1

1

从我看到你的 UICollectionViewLayout 的子类缺少一些重要的方法。由于这是一个抽象类,因此根据此处的 Apple 文档,您必须重写以下方法:

collectionViewContentSize
layoutAttributesForElementsInRect:
layoutAttributesForItemAtIndexPath:
layoutAttributesForSupplementaryViewOfKind:atIndexPath: (if your layout supports supplementary views)
layoutAttributesForDecorationViewOfKind:atIndexPath: (if your layout supports decoration views)
shouldInvalidateLayoutForBoundsChange:

这可能解释了为什么不调用回调。您可能希望切换到已实现的 FlowLayout,至少要对其进行测试。最后,关于 2),我不确定你想做什么,但不会

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

从 UICollectionViewDelegateFlowLayout 做的伎俩?根据您的单元格大小,您可能有一排有 3 个单元格占据了几乎所有空间,而另一排则有 10 个小单元格。如果不是这样,请随时澄清您的问题。

于 2013-07-22T16:46:23.183 回答