123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /*
- Copyright (c) 2013 Katsuma Tanaka
-
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
- #import "QBImagePickerAssetCell.h"
- // Views
- #import "QBImagePickerAssetView.h"
- @interface QBImagePickerAssetCell ()
- - (void)addAssetViews;
- @end
- @implementation QBImagePickerAssetCell
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier imageSize:(CGSize)imageSize numberOfAssets:(NSUInteger)numberOfAssets margin:(CGFloat)margin
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
-
- if(self) {
- self.imageSize = imageSize;
- self.numberOfAssets = numberOfAssets;
- self.margin = margin;
-
- [self addAssetViews];
- }
-
- return self;
- }
- - (void)setAssets:(NSArray *)assets
- {
- // [_assets release];
- // _assets = [assets retain];
- _assets = assets;
-
- // Set assets
- for(NSUInteger i = 0; i < self.numberOfAssets; i++) {
- QBImagePickerAssetView *assetView = (QBImagePickerAssetView *)[self.contentView viewWithTag:(1 + i)];
-
- if(i < self.assets.count) {
- assetView.hidden = NO;
-
- assetView.asset = [self.assets objectAtIndex:i];
- } else {
- assetView.hidden = YES;
- }
- }
- }
- - (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection
- {
- _allowsMultipleSelection = allowsMultipleSelection;
-
- // Set property
- for(UIView *subview in self.contentView.subviews) {
- if([subview isMemberOfClass:[QBImagePickerAssetView class]]) {
- [(QBImagePickerAssetView *)subview setAllowsMultipleSelection:self.allowsMultipleSelection];
- }
- }
- }
- - (void)dealloc
- {
- // [_assets release];
- //
- // [super dealloc];
- }
- #pragma mark - Instance Methods
- - (void)addAssetViews
- {
- // Remove all asset views
- for(UIView *subview in self.contentView.subviews) {
- if([subview isMemberOfClass:[QBImagePickerAssetView class]]) {
- [subview removeFromSuperview];
- }
- }
-
- // Add asset views
- for(NSUInteger i = 0; i < self.numberOfAssets; i++) {
- // Calculate frame
- CGFloat offset = (self.margin + self.imageSize.width) * i;
- CGRect assetViewFrame = CGRectMake(offset + self.margin, self.margin, self.imageSize.width, self.imageSize.height);
-
- // Add asset view
- QBImagePickerAssetView *assetView = [[QBImagePickerAssetView alloc] initWithFrame:assetViewFrame];
- assetView.delegate = self;
- assetView.tag = 1 + i;
- assetView.autoresizingMask = UIViewAutoresizingNone;
-
- [self.contentView addSubview:assetView];
- // [assetView release];
- }
- }
- - (void)selectAssetAtIndex:(NSUInteger)index
- {
- QBImagePickerAssetView *assetView = (QBImagePickerAssetView *)[self.contentView viewWithTag:(index + 1)];
- assetView.selected = YES;
- }
- - (void)deselectAssetAtIndex:(NSUInteger)index
- {
- QBImagePickerAssetView *assetView = (QBImagePickerAssetView *)[self.contentView viewWithTag:(index + 1)];
- assetView.selected = NO;
- }
- - (void)selectAllAssets
- {
- for(UIView *subview in self.contentView.subviews) {
- if([subview isMemberOfClass:[QBImagePickerAssetView class]]) {
- if(![(QBImagePickerAssetView *)subview isHidden]) {
- [(QBImagePickerAssetView *)subview setSelected:YES];
- }
- }
- }
- }
- - (void)deselectAllAssets
- {
- for(UIView *subview in self.contentView.subviews) {
- if([subview isMemberOfClass:[QBImagePickerAssetView class]]) {
- if(![(QBImagePickerAssetView *)subview isHidden]) {
- [(QBImagePickerAssetView *)subview setSelected:NO];
- }
- }
- }
- }
- #pragma mark - QBImagePickerAssetViewDelegate
- - (BOOL)assetViewCanBeSelected:(QBImagePickerAssetView *)assetView
- {
- return [self.delegate assetCell:self canSelectAssetAtIndex:(assetView.tag - 1)];
- }
- - (void)assetView:(QBImagePickerAssetView *)assetView didChangeSelectionState:(BOOL)selected
- {
- [self.delegate assetCell:self didChangeAssetSelectionState:selected atIndex:(assetView.tag - 1)];
- }
- @end
|