UICollectionView setScrollEnabled:是不起作用

时间:2018-11-16 18:57:51

标签: ios uicollectionview

我有一个UICollectionView,其宽度比屏幕宽度长,因此我想启用滚动以便用户可以看到它。不幸的是,对setScrollEnabled:YES的调用似乎没有任何作用。

// CODE TO SET UP

UICollectionView *tabLayerCollectionView;
UICollectionViewFlowLayout *flowLayout;

// Dont want any space between cells
flowLayout.minimumInteritemSpacing = 0; // Scrolling works if we have a space inbetween cells, but spaces are not desired.
flowLayout.minimumLineSpacing = 0;

tabLayerCollectionView = [[UICollectionView alloc] 
initWithFrame:CGRectMake(0, 40, widthOfTabLayer, noteHeight) 
collectionViewLayout:flowLayout];

// HERE - scrolling not working
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[tabLayerCollectionView setScrollEnabled:YES];

[tabLayerCollectionView setDataSource:self];
[tabLayerCollectionView setDelegate:self]; // See below for completness

[tabLayerCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:myIdentifier];
[tabLayerCollectionView setBackgroundColor:[UIColor clearColor]];

//NSLog(@"DrumKitViewController tabList count:%i",(int)tabList.count);
[self.view addSubview:tabLayerCollectionView];
[tabLayerCollectionView reloadData];

// TAB LAYER INHERITED METHODS For Completeness

    // Called once
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"numberOfItemsInSection called Tablist count:%i",(int)tabList.count);

    return tabList.count;
}

// The cell that is returned must be retrieved from a call to dequeueReusableCellWithReuseIdentifier:forIndexPath:
// Called for each indexPath row (after all layout called
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    //NSLog(@"collectionView collectionView called index section:%i row:%i",(int)indexPath.section,(int)indexPath.row);

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

    NSMutableArray *clickList = [tabList objectAtIndex:indexPath.row];

    // Background (Not at start or end)
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, noteWidth, noteHeight)];
    [backgroundImageView setImage:[UIImage imageNamed:@"bg_middle.png"]];
    [cell setBackgroundView:backgroundImageView];

    for (NSString *drumTabItem in clickList){

        //NSLog(@"clickList.count:%i drumTabItem:%@",(int)clickList.count,drumTabItem);

        // Get drumTabItem and add to itemImageView
        UIImageView *tabItemImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, noteWidth, noteHeight)];
        tabItemImageView.image = [UIImage imageNamed:drumTabItem];

        [cell.contentView addSubview:tabItemImageView];

    }

    return cell;
}



// Called for each indexPath row
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row==0){
       // This has the timing, so make bigger
        widthOfTimeComponent = noteWidth*2;
        return CGSizeMake(noteWidth*2, noteHeight); // Gives more white space
    }
    else {
        NSMutableArray *clickList = [tabList objectAtIndex:indexPath.row];
        for (NSString *item in clickList){

            if ([item isEqualToString:@"bg_middle.png"]){
                //NSLog(@"EMPTY found at indexRow:%i noOfRows:",(int)indexPath.row);

                if (countNonEmptyItems > 32.0){
                    //widthOfTabLayer = widthOfTabLayer + (noteWidth/6);
                    return CGSizeMake(noteWidth/6, noteHeight); // Needs to be shorter
                }
                else if (countNonEmptyItems > 24.0){
                    //widthOfTabLayer = widthOfTabLayer + (noteWidth/4);
                    return CGSizeMake(noteWidth/4, noteHeight); // Needs to be shorter
                }
                else if(countNonEmptyItems > 15.0){
                    //widthOfTabLayer = widthOfTabLayer + (noteWidth/3);
                    return CGSizeMake(noteWidth/3, noteHeight); // Needs to be wider
                }
                else if(countNonEmptyItems > 10.0){
                    //widthOfTabLayer = widthOfTabLayer + (noteWidth/2);
                    return CGSizeMake(noteWidth/2, noteHeight); // Needs to be wider
                }
                else{

                    return CGSizeMake(noteWidth, noteHeight); // 30x165 (
                }
            }
            else{
                return CGSizeMake(noteWidth, noteHeight); // 30x165
            }
        }
    }
    // Shouldn't get here
    return CGSizeMake(noteWidth, noteHeight); // 30x165
}

我当时假设setScrollEnabled:YES将启用滚动。关于它为什么不起作用的任何原因?

1 个答案:

答案 0 :(得分:1)

如果“内容大小”大于“视图大小”,则只能在UICollectionView(或任何其他scrollView)上进行滚动。

初始化集合视图的框架时,会将大小设置为widthOfTabLayer。我的直觉是,这实际上是您的内容大小。因此,将集合视图的宽度设置为父视图或屏幕的宽度(我们将其称为screenWidth),并将集合视图的内容大小设置为widthOfTabLayer。

tabLayerCollectionView = [[UICollectionView alloc] 
initWithFrame:CGRectMake(0, 40, screenWidth, noteHeight) 
collectionViewLayout:flowLayout];

[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
flowLayout.contentSize = widthOfTabLayer