2012-10-01 10 views
10

プログラムでUICollectionViewを使用しています。セクションインセットを設定するときに、FlowLAyoutがスクロールしないUICollectionView

私は次のように、それは、フレームの設定している:私はまた、唯一の1つのセクションを持って

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

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

UICollectionViewFlowLayoutはとして設定セクションのインセットを持っています。

高さを3,000に設定して、コレクションビューがスクロールするかどうかを確認しましたが、表示しません。コレクションビューは新しいアイテムを挿入するときに上下にスクロールしないように設定しています。

UICollectionViewUIScrollViewからサブクラス化されています。つまり、スクロールビューのコンテンツサイズをリセットしておくことができます。これは適切に機能しますか?

唯一の問題は、各行にいくつのアイテムが含まれているかなど、すべてのアイテムのサイズが分かっていれば、これが機能するということです。

各アイテムのサイズが異なる場合、どのように各行のサイズがわかりますか?すべての行サイズを追加し、コンテンツサイズの高さをself.collectionViewに増やすには、これが必要です。

EDIT

以上であっても私の提案、コンテンツのサイズをリセットし、正常に動作しません!私のコレクションビューを更新するとき、私は次のことを試してみました:

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

これはしかし非常に醜いです。これは、すべての画像のサイズが200x200pxであると仮定しているため、1行に3枚の画像が収まる(したがって3で除算される)と仮定しているためです。

さらに、私が持っている+300であっても、最後の行は私の約3/4しか見ることができず、すべてではありません。私はもっ​​とスクロールする必要があり、私はそれを見ることができるだろうが、それは再び3/4に戻る。リフレッシュするスクロールのようなものです。ドラッグするとビューが移動するだけで、ビューを元の位置に戻します。なぜこうなった?アップルがUICollectionViewをあまりにも貧弱に設計したことだけですか?これはちょっとばかげたことです... UITableViewsはコンテンツに応じてスクロールを自動的に調整します。

答えて

13

UICollectionViewに関する他の質問に答えるにあたって、私はこのデモプロジェクトを作成しました。うまくスクロールするので、何が問題なのか分かりません。最後の行を完全に見えるようにするために必要だったのは、下側のインセットのサイズを90に増やすことでした。performBatchUpdates:completion:に最後の追加された項目が表示されるように自動的にスクロールするためのcompletionメソッドも追加しましたperformSelector; withObject:afterDelay :)を使用していくつかの余分な項目を追加することに注目してください。私の画像は48x48です。私は自分のコレクションのビューをコントローラのビューの境界に設定しました。

@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); 
} 
9

あなたのスクロールの問題を悪化させるだろう3000からUICollectionViewの高さを設定する:あなたが持っているものと比較することができますので、ここでは、コードです。 UICollectionViewの高さが3000ピクセルの場合、その中に3000ピクセル以上のコンテンツがある場合はスクロールする必要があります。あなたはそれが含まれているビューの高さに設定するべきです(幅と同じです)。

も直接contentSizeを設定するべきではありません私の知る限り、代わりにあなたはUICollectionViewLayoutのサブクラスで- (CGSize)collectionViewContentSizeメソッドをオーバーライドする必要がありますが、一般的には、通常の使用のためのコンテンツのサイズを変更する必要はありません話します。

私はあなたの問題が完全にはっきりしていません。画面の数を超えるアイテムを追加すると、スクロールできません。

+0

これは私の解決策でした。私は複数の列のレイアウトでアイテム間の垂直方向の隙間を取り除いたカスタムフローレイアウトを使用していました。私はSimonが提案したようにするまで、アイテムの半分しか手に入れませんでした。私が問題があった理由は、照会されたアイテムの数が標準レイアウトに基づいているということです。 –

2

viewWillAppearメソッドのcollectionView(つまりDataSource)の要素を初期化していたため、同じ問題が発生しました。

最後に[self.collectionView reloadData];を追加しても問題ありません。

多分あなたも同じです。

関連する問題