Küçük bir örnek projesi: https://github.com/erikt/ETMultiSelect
Önce mümkün UICollectionView
birden fazla hücreyi seçmek için yapmak zorunda. Bu, allowsMultipleSelection
özelliğinin toplama görünümünde YES
olarak ayarlanarak yapılır.
görünüm denetleyicisi böyle bir şey görünecektir:
#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
birkaç görüş oluşur. İçerik görünümü, arka plan görünümü ve seçilen arka plan görünümü vardır.
resminizin benzer bir şey elde etmenin birçok yolu vardır, ama ben seçilmiş arka plan katmanı üzerinde sınır oluşturur ve arka plan sınır görünür olması için iç metin içeriğini görme bir subview ekleyin:
#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
Klon ve the sample project ile oynamak: sonuçta aşağıdaki gibi bir şeydir. Eğer kullanıcı görünümü denetleyicisi seçili ne takip etmek isteyeyim gerçek projede
,
UICollectionViewDelegate
protokolü
– collectionView:didSelectItemAtIndexPath:
yöntemde (a
NSMutableArray
gibi) bazı yapısına seçilen veri modeli varlıkları ekleyerek.
Tam cevabınız için başvuruda bulunun. – Ali