GWLPhotoGroupTableViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // GWLPhotoGroupTableViewController.m
  3. // GWLPhotoSelector
  4. //
  5. // Created by GaoWanli on 15/7/23.
  6. // Copyright (c) 2015年 GWL. All rights reserved.
  7. //
  8. #import "GWLPhotoGroupTableViewController.h"
  9. #import "GWLPhotoGroupDetailController.h"
  10. @interface GWLPhotoGroupCell : UITableViewCell
  11. @property(nonatomic, strong) GWLPhotoGroup *photoGroup;
  12. + (instancetype)cellWithTableView:(UITableView *)tableView;
  13. @property(nonatomic, weak) UIImageView *iconView;
  14. @property(nonatomic, weak) UILabel *infoLabel;
  15. @end
  16. @implementation GWLPhotoGroupCell
  17. + (instancetype)cellWithTableView:(UITableView *)tableView {
  18. static NSString *ID = @"photoGroupCell";
  19. GWLPhotoGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  20. if (cell == nil)
  21. cell = [[GWLPhotoGroupCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
  22. return cell;
  23. }
  24. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  25. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
  26. [self makeCellSubviews];
  27. return self;
  28. }
  29. - (void)makeCellSubviews {
  30. UIImageView *iconView = [[UIImageView alloc]init];
  31. iconView.contentMode = UIViewContentModeScaleAspectFit;
  32. iconView.backgroundColor = [UIColor clearColor];
  33. self.iconView = iconView;
  34. [self addSubview:iconView];
  35. UILabel *infoLabel = [[UILabel alloc]init];
  36. infoLabel.textAlignment = NSTextAlignmentLeft;
  37. self.infoLabel = infoLabel;
  38. [self addSubview:infoLabel];
  39. }
  40. - (void)layoutSubviews {
  41. [super layoutSubviews];
  42. CGFloat padding = 10;
  43. CGFloat controlWH = 44;
  44. CGFloat marginLR = 15;
  45. CGFloat marginTB = (CGRectGetHeight(self.frame) - controlWH) * 0.5;
  46. _iconView.frame = CGRectMake(marginLR, marginTB, controlWH, controlWH);
  47. _infoLabel.frame = CGRectMake(CGRectGetMaxX(_iconView.frame) + padding, marginTB, CGRectGetWidth(self.frame) - CGRectGetMaxX(_iconView.frame) - marginLR, controlWH);
  48. }
  49. - (void)setPhotoGroup:(GWLPhotoGroup *)photoGroup {
  50. _photoGroup = photoGroup;
  51. [self.iconView setImage:photoGroup.groupIcon];
  52. self.infoLabel.text = [NSString stringWithFormat:@"%@(%zd)",photoGroup.groupName,photoGroup.photoALAssets.count];
  53. }
  54. @end
  55. @interface GWLPhotoGroupTableViewController ()
  56. @property(nonatomic, weak) UIView *errorMessageView;
  57. @property(nonatomic, strong) GWLPhotoGroupDetailController *photoGroupDetailController;
  58. @end
  59. @implementation GWLPhotoGroupTableViewController
  60. - (void)viewDidLoad {
  61. [super viewDidLoad];
  62. self.title = @"相簿";
  63. self.tableView.tableFooterView = [[UIView alloc]init];
  64. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelBtnDidClick)];
  65. }
  66. - (void)cancelBtnDidClick {
  67. [self dismissViewControllerAnimated:YES completion:nil];
  68. }
  69. - (void)showErrorMessageView {
  70. self.errorMessageView.hidden = NO;
  71. }
  72. /**设置数据*/
  73. - (void)setPhotoGroupArray:(NSArray *)photoGroupArray{
  74. _photoGroupArray = photoGroupArray;
  75. [self.tableView reloadData];
  76. }
  77. #pragma mark - UITableViewDataSource
  78. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  79. return 1;
  80. }
  81. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  82. return self.photoGroupArray.count;
  83. }
  84. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  85. GWLPhotoGroupCell *cell = [GWLPhotoGroupCell cellWithTableView:tableView];
  86. GWLPhotoGroup *photoGroup = self.photoGroupArray[indexPath.section];
  87. if (photoGroup.photoALAssets.count == 0) {
  88. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  89. }else {
  90. cell.selectionStyle = UITableViewCellSelectionStyleDefault;
  91. }
  92. cell.photoGroup = photoGroup;
  93. return cell;
  94. }
  95. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  96. return kGWLPhotoSelector_Cell_Height;
  97. }
  98. #pragma mark - UITableViewDelegate
  99. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  100. GWLPhotoGroup *photoGroup = self.photoGroupArray[indexPath.section];
  101. if (photoGroup.photoALAssets.count == 0)
  102. return;
  103. GWLPhotoGroupDetailController *photoGroupDetailController;
  104. if (!self.canMultiAlbumSelect) {
  105. for (GWLPhotoGroup *photoGroup in self.photoGroupArray) {
  106. for (GWLPhotoALAssets *photoALAssets in photoGroup.photoALAssets) {
  107. photoALAssets.selected = NO;
  108. }
  109. }
  110. photoGroupDetailController = [[GWLPhotoGroupDetailController alloc]init];
  111. photoGroupDetailController.maxCount = self.maxCount;
  112. photoGroupDetailController.block = self.block;
  113. }else {
  114. if (!_photoGroupDetailController) {
  115. _photoGroupDetailController = [[GWLPhotoGroupDetailController alloc]init];
  116. _photoGroupDetailController.maxCount = self.maxCount;
  117. _photoGroupDetailController.block = self.block;
  118. }
  119. photoGroupDetailController = _photoGroupDetailController;
  120. }
  121. photoGroupDetailController.photoALAssets = photoGroup.photoALAssets;
  122. [self.navigationController pushViewController:photoGroupDetailController animated:YES];
  123. }
  124. #pragma mark - getter && setter
  125. - (void)setMaxCount:(NSInteger)maxCount {
  126. _maxCount = maxCount;
  127. }
  128. - (UIView *)errorMessageView {
  129. if (!_errorMessageView) {
  130. UIView *errorMessageView = [[UIView alloc]init];
  131. errorMessageView.backgroundColor = [UIColor whiteColor];
  132. errorMessageView.frame = self.view.bounds;
  133. errorMessageView.hidden = YES;
  134. self.errorMessageView = errorMessageView;
  135. [self.view addSubview:errorMessageView];
  136. UILabel *msgLabel = [[UILabel alloc]init];
  137. msgLabel.text = kGWLPhotoSelector_ErrorMessageText;
  138. msgLabel.backgroundColor = [UIColor clearColor];
  139. msgLabel.font = [UIFont systemFontOfSize:15];
  140. msgLabel.textAlignment = NSTextAlignmentCenter;
  141. msgLabel.textColor = [UIColor lightGrayColor];
  142. CGFloat msgLabelHeight = 15;
  143. msgLabel.frame = CGRectMake(0, (CGRectGetHeight(errorMessageView.frame) - msgLabelHeight - 64) * 0.5 , CGRectGetWidth(errorMessageView.frame), msgLabelHeight);
  144. [errorMessageView addSubview:msgLabel];
  145. _errorMessageView = errorMessageView;
  146. }
  147. return _errorMessageView;
  148. }
  149. @end