我有一個UICollectionView
設置UICollectionViewDataSource
,目前提供六個項目。 這些比填寫屏幕所需要的要少。問題是我的收藏視圖只在有足夠的項目填滿屏幕時滾動(用10,20測試)。 當顯示更少的項目,它甚至不會做這個反彈動畫,我試圖得到它只是固定的。UICollectionView不滾動
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCollectionViewData) name:UIDocumentStateChangedNotification object:nil];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(160, 100);
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.bounces = YES;
[self.view addSubview:self.collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.collectionViewData count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
Expense *expense = [self.collectionViewData objectAtIndex:indexPath.row];
UILabel *label = [[UILabel alloc]initWithFrame:cell.bounds];
label.text = expense.value;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"Miso-Bold" size:30];
label.textAlignment = NSTextAlignmentCenter;
[cell addSubview:label];
cell.backgroundColor = [UIColor colorWithRed:1 - (indexPath.row/30.0f) green:0 blue:1 alpha:1];
return cell;
}
感謝您的幫助!
這是正確的答案 - OP應該接受這個答案。 –
偉大的技巧感謝 – Fattie
謝謝,它像一個魅力:) – evya