0

我正在尝试在一个视图中显示多个 UICollectionView。该视图是折叠手风琴样式视图,使用此自定义 AccordionView 控件

目标是显示图像缩略图的多个部分,其中每个部分包含一个 UICollectionView,如下所示:

视图的设计

数据以self.wallpaperCollectionArray下面的 NSArray 形式提供,其中wallpaper_id是缩略图图像的路径:

(
    {
        name = "Bold Statement";
        "wallpaper_id" = (
            "bs-better",
            "bs-look-forward",
            "bs-now-never",
            "bs-stay-hype",
            "bs-you-awesome"
        );

    },
    {
        name = "Dreamcatcher";
        "wallpaper_id" = (
            "dc-disney",
            "dc-fear",
            "dc-paulo",
            "dc-quit"
        );
    },
    {
        name = "Goods from the good";
        "wallpaper_id" = (
            "gg-get-hurt",
            "gg-jobs",
            "gg-lennon",
            "gg-oprah"
        );
    },
    {
        name = "Random Set";
        "wallpaper_id" = (
            "rs-face",
            "rs-god-knows",
            "rs-good",
            "rs-holtz",
            "rs-positive",
            "rs-read",
            "rs-think"
        );
    },
    {
        name = Traveling;
        "wallpaper_id" = (
            "tr-miller"
        );
    }
)

这是我生成 UICollectionView 的代码:

- (void)loadView {
    UIView *libraryView = [UIView new];

    CGFloat maxWidth = [[UIScreen mainScreen] bounds].size.width;
    CGFloat maxHeight = [[UIScreen mainScreen] bounds].size.height;

    // Setup accordion
    self.libraryAccordionView = [[AccordionView alloc] initWithFrame:CGRectMake(0, 0, maxWidth, maxHeight)];

    // Re-usable layout for the UICollectionView(s) inside the for loop
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(thumbnailSize, thumbnailSize);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 1;

    // Looping through the array contents:
    int i = 0;
    for(NSDictionary *currentCollectionDictionary in self.wallpaperCollectionArray) {
        NSArray *wallpaperIDs = [currentCollectionDictionary valueForKeyPath:@"wallpaper_id"];
        NSInteger numberOfWallpapers = [wallpaperIDs count];

        // One row contains 4 thumbnails, while each thumbnail's height is 79.
        // So, collectionView height = (total item / 4 + 1) * 79
        NSInteger collectionViewHeight = (numberOfWallpapers / 4 + 1) * thumbnailSize;
        UICollectionView *currentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, maxWidth, collectionViewHeight)
                                                                     collectionViewLayout:layout];
        [currentCollectionView setDataSource:(id)self];
        [currentCollectionView setDelegate:(id)self];
        NSString *cellIdentifier = [NSString stringWithFormat:@"cell_%d", i];
        [currentCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];

        // Trick to let the delegate and data source functions deal with the multiple UICollectionView's
        [currentCollectionView setTag:i];

        // * --- Create header --- * //
        UIButton *currentHeader = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
        NSString *currentHeaderTitle = [currentCollectionDictionary valueForKeyPath:@"name"];
        [currentHeader setTitle:currentHeaderTitle forState:UIControlStateNormal];

        [self.libraryAccordionView addHeader:currentHeader withView:currentCollectionView];
        i++;
    }

    [libraryView addSubview:self.libraryAccordionView]
    [self setView:libraryView];
}

数据源方法:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger index = collectionView.tag;
    NSInteger numberOfWallpaper = [[[self.wallpaperCollectionArray objectAtIndex:index] valueForKeyPath:@"wallpaper_id"] count];
    NSLog(@"index is: %d --- # of item is: %d", index, numberOfWallpaper);
    return numberOfWallpaper;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = collectionView.tag;
    NSString *cellIdentifier = [NSString stringWithFormat:@"cell_%d", index];

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier
                                                                           forIndexPath:indexPath];

    NSString *thumbnailName = [[[self.wallpaperCollectionArray objectAtIndex:index] valueForKeyPath:@"wallpaper_id"] objectAtIndex:indexPath.row];

    UIImageView *wallpaperImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:thumbnailName]];
    [cell addSubview:wallpaperImage];
    return cell;
}

问题是,所有 UICollectionView 都只显示一个缩略图,尽管它们似乎正在加载正确的缩略图:

结果 结果

起初我意识到问题可能出在- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section方法上。

但是,在这个测试NSLog(@"index is: %d --- # of item is: %d", index, numberOfWallpaper);中,结果numberOfWallpaper是正确的:

2013-10-04 08:50:43.992 quo.mk.e[15046:11603] index is: 4 --- # of item is: 1
2013-10-04 08:50:43.993 quo.mk.e[15046:11603] index is: 3 --- # of item is: 7
2013-10-04 08:50:43.994 quo.mk.e[15046:11603] index is: 2 --- # of item is: 4
2013-10-04 08:50:43.994 quo.mk.e[15046:11603] index is: 1 --- # of item is: 4
2013-10-04 08:50:43.995 quo.mk.e[15046:11603] index is: 0 --- # of item is: 5

所以,我返回正确数量的缩略图numberOfItemsInSection。但是,为什么 UICollectionView 只显示一个缩略图?

4

1 回答 1

4

原来问题在于布局的重用。使用多个 UICollectionView,我们需要创建自己的单独布局。由于某些原因。

在循环内移动布局创建可以- (void)loadView解决问题:

int i = 0;
for(NSDictionary *currentCollectionDictionary in self.wallpaperCollectionArray) {
    NSArray *wallpaperIDs = [currentCollectionDictionary valueForKeyPath:@"wallpaper_id"];
    NSInteger numberOfWallpapers = [wallpaperIDs count];

    // Layouting
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(thumbnailSize, thumbnailSize);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 1;

    ...
于 2013-10-04T02:13:53.083 回答