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