gpt4 book ai didi

ios - UICollectionView setScrollEnabled :YES not working

转载 作者:行者123 更新时间:2023-11-28 23:41:18 28 4
gpt4 key购买 nike

我有一个宽度比屏幕宽度长的 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 会启用滚动。为什么它不起作用?有什么原因吗?

最佳答案

如果内容大小大于 View 大小,则滚动仅适用于 UICollectionView(或任何其他 ScrollView )。

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

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

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

关于ios - UICollectionView setScrollEnabled :YES not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53343860/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com