QBImagePickerAssetCell.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (c) 2013 Katsuma Tanaka
  3. 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:
  4. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5. 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.
  6. */
  7. #import "QBImagePickerAssetCell.h"
  8. // Views
  9. #import "QBImagePickerAssetView.h"
  10. @interface QBImagePickerAssetCell ()
  11. - (void)addAssetViews;
  12. @end
  13. @implementation QBImagePickerAssetCell
  14. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier imageSize:(CGSize)imageSize numberOfAssets:(NSUInteger)numberOfAssets margin:(CGFloat)margin
  15. {
  16. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  17. if(self) {
  18. self.imageSize = imageSize;
  19. self.numberOfAssets = numberOfAssets;
  20. self.margin = margin;
  21. [self addAssetViews];
  22. }
  23. return self;
  24. }
  25. - (void)setAssets:(NSArray *)assets
  26. {
  27. // [_assets release];
  28. // _assets = [assets retain];
  29. _assets = assets;
  30. // Set assets
  31. for(NSUInteger i = 0; i < self.numberOfAssets; i++) {
  32. QBImagePickerAssetView *assetView = (QBImagePickerAssetView *)[self.contentView viewWithTag:(1 + i)];
  33. if(i < self.assets.count) {
  34. assetView.hidden = NO;
  35. assetView.asset = [self.assets objectAtIndex:i];
  36. } else {
  37. assetView.hidden = YES;
  38. }
  39. }
  40. }
  41. - (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection
  42. {
  43. _allowsMultipleSelection = allowsMultipleSelection;
  44. // Set property
  45. for(UIView *subview in self.contentView.subviews) {
  46. if([subview isMemberOfClass:[QBImagePickerAssetView class]]) {
  47. [(QBImagePickerAssetView *)subview setAllowsMultipleSelection:self.allowsMultipleSelection];
  48. }
  49. }
  50. }
  51. - (void)dealloc
  52. {
  53. // [_assets release];
  54. //
  55. // [super dealloc];
  56. }
  57. #pragma mark - Instance Methods
  58. - (void)addAssetViews
  59. {
  60. // Remove all asset views
  61. for(UIView *subview in self.contentView.subviews) {
  62. if([subview isMemberOfClass:[QBImagePickerAssetView class]]) {
  63. [subview removeFromSuperview];
  64. }
  65. }
  66. // Add asset views
  67. for(NSUInteger i = 0; i < self.numberOfAssets; i++) {
  68. // Calculate frame
  69. CGFloat offset = (self.margin + self.imageSize.width) * i;
  70. CGRect assetViewFrame = CGRectMake(offset + self.margin, self.margin, self.imageSize.width, self.imageSize.height);
  71. // Add asset view
  72. QBImagePickerAssetView *assetView = [[QBImagePickerAssetView alloc] initWithFrame:assetViewFrame];
  73. assetView.delegate = self;
  74. assetView.tag = 1 + i;
  75. assetView.autoresizingMask = UIViewAutoresizingNone;
  76. [self.contentView addSubview:assetView];
  77. // [assetView release];
  78. }
  79. }
  80. - (void)selectAssetAtIndex:(NSUInteger)index
  81. {
  82. QBImagePickerAssetView *assetView = (QBImagePickerAssetView *)[self.contentView viewWithTag:(index + 1)];
  83. assetView.selected = YES;
  84. }
  85. - (void)deselectAssetAtIndex:(NSUInteger)index
  86. {
  87. QBImagePickerAssetView *assetView = (QBImagePickerAssetView *)[self.contentView viewWithTag:(index + 1)];
  88. assetView.selected = NO;
  89. }
  90. - (void)selectAllAssets
  91. {
  92. for(UIView *subview in self.contentView.subviews) {
  93. if([subview isMemberOfClass:[QBImagePickerAssetView class]]) {
  94. if(![(QBImagePickerAssetView *)subview isHidden]) {
  95. [(QBImagePickerAssetView *)subview setSelected:YES];
  96. }
  97. }
  98. }
  99. }
  100. - (void)deselectAllAssets
  101. {
  102. for(UIView *subview in self.contentView.subviews) {
  103. if([subview isMemberOfClass:[QBImagePickerAssetView class]]) {
  104. if(![(QBImagePickerAssetView *)subview isHidden]) {
  105. [(QBImagePickerAssetView *)subview setSelected:NO];
  106. }
  107. }
  108. }
  109. }
  110. #pragma mark - QBImagePickerAssetViewDelegate
  111. - (BOOL)assetViewCanBeSelected:(QBImagePickerAssetView *)assetView
  112. {
  113. return [self.delegate assetCell:self canSelectAssetAtIndex:(assetView.tag - 1)];
  114. }
  115. - (void)assetView:(QBImagePickerAssetView *)assetView didChangeSelectionState:(BOOL)selected
  116. {
  117. [self.delegate assetCell:self didChangeAssetSelectionState:selected atIndex:(assetView.tag - 1)];
  118. }
  119. @end