2012-10-30 6 views
12

UICollectionViewを作成したいのは初めてのことです。これは、私はそれが見えるようにする方法である:UICollectionViewのボーダーを設定する

enter image description here

私はいくつかのチュートリアルを読んで、私はそれが正確にどのように動作するかを知っています。 UICollectionのセルには、上、下、左、右の境界線があります。 Collection Viewでこれらの種類の境界をどのように設定できるか知っていますか?

ご覧のとおり、2つのアイテムは赤色で選択されています。 UICollectionViewで複数の選択項目を持つことは可能ですか?はいの場合は、チュートリアルを送ってください。ここ

答えて

26

小さなプロジェクト例:https://github.com/erikt/ETMultiSelect

まず、UICollectionViewで複数のセルを選択することを可能にする必要があります。これは、コレクションビューのにallowsMultipleSelectionプロパティを設定することによって行われます。

ビューコントローラは、次のようになります

#import "ETViewController.h" 
#import "ETCellView.h" 

@implementation ETViewController 

static NSString *cellIdentifier = @"cvCell"; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Register our custom collection view cell 
    [self.collectionView registerClass:ETCellView.class forCellWithReuseIdentifier:cellIdentifier]; 

    // Make it possible to select multiple cells 
    self.collectionView.allowsMultipleSelection = YES; 
} 

#pragma mark - UICollectionViewDataSource 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
    return 1; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return 10; 
} 

#pragma mark - UICollectionViewDelegate 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    ETCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    return cell; 
} 

@end 

UICollectionViewCellは、いくつかのビューで構成されています。コンテンツビュー、バックグラウンドビュー、および選択されたバックグラウンドビューがあります。

は、あなたの写真に似た何かを達成するための多くの方法がありますが、私は、選択した背景レイヤ上の境界線を設定すると背景の境界線が見えるようにはめ込みだコンテンツビューにサブビューを追加します。

#import "ETCellView.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation ETCellView 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.restorationIdentifier = @"cvCell"; 
     self.backgroundColor = [UIColor clearColor]; 
     self.autoresizingMask = UIViewAutoresizingNone; 

     CGFloat borderWidth = 3.0f; 
     UIView *bgView = [[UIView alloc] initWithFrame:frame]; 
     bgView.layer.borderColor = [UIColor redColor].CGColor; 
     bgView.layer.borderWidth = borderWidth; 
     self.selectedBackgroundView = bgView; 

     CGRect myContentRect = CGRectInset(self.contentView.bounds, borderWidth, borderWidth); 

     UIView *myContentView = [[UIView alloc] initWithFrame:myContentRect]; 
     myContentView.backgroundColor = [UIColor whiteColor]; 
     myContentView.layer.borderColor = [UIColor colorWithWhite:0.5f alpha:1.0f].CGColor; 
     myContentView.layer.borderWidth = borderWidth; 
     [self.contentView addSubview:myContentView]; 
    } 
    return self; 
} 

@end 

iPhone screen shot

クローンとthe sample projectと遊ぶ:結果はこのようなものです。あなたはUICollectionViewDelegateプロトコル上– collectionView:didSelectItemAtIndexPath:方法で(NSMutableArrayのような)いくつかの構造体に、選択されたデータモデルエンティティを追加することにより、ユーザは、ビューコントローラで選択したものを追跡したいと思う実際のプロジェクトで

+0

あなたの完全な回答に感謝します。 – Ali

関連する問題