6

これは UITableViewCells にとってかなり簡単であることはわかっていますが、UICollectionView を使用してこれにアプローチする方法がわかりません。

編集。説明のための写真。セルのテキスト コンテンツはここでは同じではありませんが、同じである必要があります。横向き:

縦向き:

ここに画像の説明を入力

メソッドのインデックス パスの行プロパティに基づいて、セルのテキスト ラベルの色をセルの背景色に素朴に切り替えようとしましたcellForItemAtIndexPath:。ただし、Index Path の行プロパティは、実際には UICollectionViewFlowLayout の行ではありません。

4

5 に答える 5

1

フロー レイアウトをサブクラス化せずにこれを行う一般的な方法はないと思います。より具体的なケースでは、整数演算とモジュラス演算子を組み合わせて使用​​して、indexPath から「行」を取得できます。したがって、各行に 5 つの項目がある場合、次の式から 0 または 1 を返すことができます。

NSInteger rowTest = (indexPath.row / 5) % 2;

この値をテストして、それに応じて色を変更してください。もちろん、行ごとに異なる数の項目があるため、ローテーションで式を変更する必要があります。

于 2013-08-19T15:51:48.610 に答える
0

このような何かがそれを行う必要があります。微調整などが必要になる場合があります。いくつかの変更が必要なセクションを使用していないと仮定すると..

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end

#import "ViewController.h"

#define WIDTH   200
#define HEIGHT  200
#define XMARGIN 50
#define YMARGIN 20

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (assign, nonatomic) int numberOfCols;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UINib* cellNib = [UINib nibWithNibName:@"CollectionCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"CollectionCell"];    
}

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

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    NSLog(@"Row number %d",indexPath.row);
    int rem = (indexPath.row / self.numberOfCols);
    NSLog(@"col number %d", rem);
    if ( rem % 2 == 0 )
        cell.backgroundColor = [UIColor yellowColor];
    else
        cell.backgroundColor = [UIColor greenColor];
    return cell;
}

#pragma mark – UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.numberOfCols = (collectionView.frame.size.width - (XMARGIN*2)) / (WIDTH);
    NSLog(@"Number of cols is %d",self.numberOfCols);

    return CGSizeMake(WIDTH,HEIGHT);
}

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

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.collectionView reloadData];
}
于 2013-08-19T20:54:21.533 に答える
0

行を取得するには、indexPath.section プロパティを使用します。

于 2013-08-19T15:32:30.993 に答える
0

これは厄介で、ヘッダーがあると壊れる可能性がありますが、少なくともこれでうまくいきます。cellForItemAtIndexPath で...

// dequeue cell

CGFloat originY = cell.frame.origin.y;

// if there are headers, then grab the header view and...
originY -= header.bounds.size.height * indexPath.section;

NSInteger row = originY / cell.bounds.size.height;
BOOL evenRow = row%2 == 0;
于 2013-08-19T21:13:44.737 に答える