57

私は、現在 6 つのアイテムを提供する をUICollectionViewセットアップしています。UICollectionViewDataSourceこれらは、画面を埋めるのに必要な数よりも少なくなっています。問題は、私のコレクション ビューは、画面を埋めるのに十分なアイテムがある場合にのみスクロールすることです (10、20 でテスト済み)。より少ないアイテムを表示すると、私が取得しようとしているこのバウンス アニメーションも実行されません。修正されました。

- (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;
}

ご協力いただきありがとうございます!

4

3 に答える 3

187

bounces、その名前にもかかわらず、設定する適切なプロパティではありません。alwaysBounceVerticaland / orも設定する必要がありますalwaysBounceHorizontal。ドキュメントから:

このプロパティが YES に設定され、バウンスが YES の場合、コンテンツがスクロール ビューの境界よりも小さい場合でも、垂直方向のドラッグが許可されます。デフォルト値は NO です。


IBの紛らわしい名前に注意してください.. https://stackoverflow.com/a/18391029/294884

于 2013-06-06T16:16:57.120 に答える
6

の高さを のUICollectionViewサイズに設定UIViewすると、スクロールの問題が無効になります。高さが 568 ピクセルの場合、UICollectionView568 ピクセル以上のコンテンツが含まれている場合にのみスクロールする必要があります。それが含まれているビューの高さに設定する必要があります (幅と同じ)。

お役に立てば幸いです。

于 2013-04-17T20:25:44.950 に答える