gpt4 book ai didi

objective-c - 设置部分插入时,带有 FlowLayout 的 UICollectionView 不滚动

转载 作者:技术小花猫 更新时间:2023-10-29 10:58:10 24 4
gpt4 key购买 nike

我正在以编程方式使用 UICollectionView。

我将它的框架设置如下:

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 3000) collectionViewLayout:flowLayout];

我的 UICollectionViewFlowLayout 有一个部分插图设置为:

flowLayout.sectionInset = UIEdgeInsetsMake(20.0, 20.0, 100.0, 20.0);

我也只有 1 个部分。

我已将高度设置为 3,000 以查看 Collection View 是否会滚动但它不会。我将其设置为该值,因为当我插入新项目时, Collection View 似乎不会向上或向下滚动。

UICollectionViewUIScrollView 的子类,这意味着我可以不断重置 ScrollView 内容大小。这能正常工作吗?

唯一的问题是,如果我知道所有项目的大小(包括每行有多少项目),这将起作用。

如果每个项目都有不同的大小,我如何找出每一行的大小?我需要它来添加所有行大小并增加 self.collectionView 的内容大小高度。

编辑

即使是我上面的建议,重置内容大小,也不能正常工作!更新我的 Collection View 时,我尝试了以下操作:

 int numberOfRows = self.dataArray.count / 3;
self.collectionView.contentSize = CGSizeMake(self.view.frame.size.width, (numberOfRows * 200) + 300);

虽然这很丑陋。这是因为它假设我所有的图像都是 200x200px 大小,每行适合 3 张图像(因此除以 3)。

此外,即使我有 +300,最后一行我也只能看到大约 3/4 而不是全部。我必须滚动更多才能看到它,但它又回到了 3/4。它有点像要刷新的滚动条, View 只有在您拖动它时才会移动,而当您离开时它会撞回原来的位置。为什么会这样?难道只是Apple把UICollectionView设计的太差了?这有点荒谬……UITableViews 会根据内容自动调整滚动条。

最佳答案

在回答您关于 UICollectionView 的一些其他问题时,我创建了这个演示项目,它滚动得很好,所以我不知道您遇到了什么问题。要使最后一行完全可见,我唯一需要做的就是将底部插图的大小增加到 90。我还在 performBatchUpdates:completion: 中添加了一个完成方法来自动滚动,以便最后添加的项目可见(请注意,我使用 performSelector;withObject:afterDelay:) 添加了一些额外的项目。我的图像是 48x48,我只是将我的 Collection View 设置为我的 Controller View 的边界。这是代码,您可以将其与现有代码进行比较:

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> {
NSMutableArray *newData;
}
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *results;
@end

@implementation ViewController

- (void)viewDidLoad {
self.results = [NSMutableArray array];
int i = 11;
while (i<91) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"New_PICT00%d.jpg",i]];
[self.results addObject:image];
i++;
}

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
//[self.collectionView registerClass:[RDCell class] forCellWithReuseIdentifier:@"myCell"];
[self.collectionView reloadData];

[self performSelector:@selector(addNewImages:) withObject:nil afterDelay:3];
}

-(void)addNewImages:(id)sender {
newData = [NSMutableArray array];
int i = 102;
while (i<111) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"New_PICT0%d.jpg",i]];
[newData addObject:image];
i++;
}
[self addNewCells];
}

-(void)addNewCells {
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
[self.collectionView performBatchUpdates:^{
int resultsSize = [self.results count];
[self.results addObjectsFromArray:newData];
for (int i = resultsSize; i < resultsSize + newData.count; i++)
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
[self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
}
completion: ^(BOOL finished){
[self.collectionView scrollToItemAtIndexPath:arrayWithIndexPaths.lastObject atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
}];
}


- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return [self.results count];
}

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
//cell.iv.image = [self.results objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
UIImage *image = [self.results objectAtIndex:indexPath.row];
return CGSizeMake(image.size.width, image.size.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 10, 90, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Selected Image is Item %d",indexPath.row);
}

关于objective-c - 设置部分插入时,带有 FlowLayout 的 UICollectionView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12679182/

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