CGXVerticalMenuCollectionView.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //
  2. // CGXVerticalMenuCollectionView.m
  3. // CGXVerticalMenuView-OC
  4. //
  5. // Created by CGX on 2018/05/01.
  6. // Copyright © 2019 CGX. All rights reserved.
  7. //
  8. #import "CGXVerticalMenuCollectionView.h"
  9. #import "CGXVerticalMenuRoundFlowLayout.h"
  10. #import "CGXVerticalMenuCollectionSectionTextView.h"
  11. #import "CGXVerticalMenuCollectionCell.h"
  12. @interface CGXVerticalMenuCollectionView()<CGXVerticalMenuRoundFlowLayoutDelegate>
  13. @property (nonatomic, strong,readwrite) NSMutableArray <CGXVerticalMenuCollectionSectionModel *> *dataArray;
  14. @property (nonatomic, strong,readwrite) CGXVerticalMenuCustomCollectionView *collectionView;
  15. @end
  16. @implementation CGXVerticalMenuCollectionView
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. [self initializeData];
  22. [self initializeViews];
  23. }
  24. return self;
  25. }
  26. - (instancetype)initWithCoder:(NSCoder *)coder
  27. {
  28. self = [super initWithCoder:coder];
  29. if (self) {
  30. [self initializeData];
  31. [self initializeViews];
  32. }
  33. return self;
  34. }
  35. - (void)initializeData
  36. {
  37. self.dataArray = [NSMutableArray array];
  38. self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
  39. }
  40. - (CGXVerticalMenuRoundFlowLayout *)preferredLayout
  41. {
  42. CGXVerticalMenuRoundFlowLayout *layout = [[CGXVerticalMenuRoundFlowLayout alloc] init];
  43. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  44. layout.isRoundEnabled = YES;
  45. layout.isCalculateHeader = YES;
  46. layout.isCalculateFooter = YES;
  47. return layout;
  48. }
  49. - (void)initializeViews
  50. {
  51. self.collectionView = [[CGXVerticalMenuCustomCollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[self preferredLayout]];
  52. self.collectionView.backgroundColor = self.backgroundColor;
  53. self.collectionView.showsHorizontalScrollIndicator = NO;
  54. self.collectionView.showsVerticalScrollIndicator = NO;
  55. self.collectionView.scrollsToTop = NO;
  56. self.collectionView.dataSource = self;
  57. self.collectionView.delegate = self;
  58. self.collectionView.alwaysBounceVertical = YES;
  59. [self.collectionView registerClass:[CGXVerticalMenuCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([CGXVerticalMenuCollectionCell class])];
  60. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
  61. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
  62. [self.collectionView registerClass:[CGXVerticalMenuCollectionSectionTextView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([CGXVerticalMenuCollectionSectionTextView class])];
  63. if (@available(iOS 11.0, *)) {
  64. self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  65. }
  66. self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
  67. [self addSubview:self.collectionView];
  68. [self.collectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
  69. }
  70. - (void)layoutSubviews
  71. {
  72. [super layoutSubviews];
  73. self.collectionView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
  74. [self.collectionView setContentOffset:CGPointMake(0, 0) animated:NO];
  75. [self.collectionView reloadData];
  76. }
  77. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  78. return self.dataArray.count;
  79. }
  80. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  81. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  82. return sectionModel.rowArray.count;
  83. }
  84. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  85. {
  86. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  87. return UIEdgeInsetsMake(sectionModel.insets.top, sectionModel.insets.left+sectionModel.borderInsets.left, sectionModel.insets.bottom+sectionModel.borderInsets.bottom, sectionModel.insets.right+sectionModel.borderInsets.right);
  88. }
  89. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  90. {
  91. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  92. return sectionModel.minimumLineSpacing;
  93. }
  94. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  95. {
  96. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  97. return sectionModel.minimumInteritemSpacing;
  98. }
  99. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  100. {
  101. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[indexPath.section];
  102. UIEdgeInsets insets = sectionModel.insets;
  103. CGFloat space0 = sectionModel.borderInsets.left + sectionModel.borderInsets.right;
  104. CGFloat space1 = insets.left + insets.right;
  105. CGFloat space2 = sectionModel.minimumInteritemSpacing;
  106. CGFloat width = (CGRectGetWidth(self.frame)-space1-space0-(sectionModel.rowCount-1)*space2)/sectionModel.rowCount;
  107. CGFloat height = width;
  108. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(categoryRightView:sizeForItemAtSection:ItemWidth:)]) {
  109. height = [self.dataSouce categoryRightView:self sizeForItemAtSection:indexPath.section ItemWidth:width];
  110. }
  111. return CGSizeMake(floor(width), height);
  112. }
  113. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  114. {
  115. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  116. if (sectionModel.footerHeight==0) {
  117. return CGSizeMake(collectionView.frame.size.width, sectionModel.footerHeight);
  118. }
  119. return CGSizeMake(collectionView.frame.size.width, sectionModel.footerHeight+sectionModel.borderInsets.bottom);
  120. }
  121. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  122. {
  123. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  124. if (sectionModel.headerHeight==0) {
  125. return CGSizeMake(collectionView.frame.size.width, sectionModel.headerHeight);
  126. }
  127. return CGSizeMake(collectionView.frame.size.width, sectionModel.headerHeight+sectionModel.borderInsets.top);
  128. }
  129. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  130. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[indexPath.section];
  131. if (kind == UICollectionElementKindSectionHeader) {
  132. UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
  133. [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  134. [obj removeFromSuperview];
  135. }];
  136. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(categoryRightView:KindHeadAtIndexPath:)] && [self.dataSouce categoryRightView:self KindHeadAtIndexPath:indexPath]) {
  137. UICollectionReusableView *headerView =[self.dataSouce categoryRightView:self KindHeadAtIndexPath:indexPath];
  138. CGRect frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
  139. if (view.frame.size.height > sectionModel.borderInsets.top) {
  140. frame.origin.y = sectionModel.borderInsets.top;
  141. frame.size.height = view.frame.size.height-sectionModel.borderInsets.top;
  142. }
  143. if (sectionModel.roundModel.isCalculateHeader) {
  144. frame.origin.x = sectionModel.borderInsets.left;
  145. frame.size.width = view.frame.size.width-sectionModel.borderInsets.left-sectionModel.borderInsets.right;
  146. }
  147. headerView.frame = frame;
  148. headerView.backgroundColor = sectionModel.headerBgColor;
  149. [view addSubview:headerView];
  150. } else{
  151. CGXVerticalMenuCollectionSectionTextView *textView = [[CGXVerticalMenuCollectionSectionTextView alloc] init];
  152. textView.frame = CGRectMake(0, 0, CGRectGetWidth(collectionView.frame), sectionModel.headerHeight);;
  153. CGRect frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
  154. if (view.frame.size.height > sectionModel.borderInsets.top) {
  155. frame.origin.y = sectionModel.borderInsets.top;
  156. frame.size.height = view.frame.size.height-sectionModel.borderInsets.top;
  157. }
  158. if (sectionModel.roundModel.isCalculateHeader) {
  159. frame.origin.x = sectionModel.borderInsets.left;
  160. frame.size.width = view.frame.size.width-sectionModel.borderInsets.left-sectionModel.borderInsets.right;
  161. }
  162. textView.frame = frame;
  163. textView.backgroundColor = sectionModel.headerBgColor;
  164. [textView updateWithTextModel:sectionModel.headNameModel];
  165. [view addSubview:textView];
  166. }
  167. return view;
  168. } else {
  169. UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
  170. [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  171. [obj removeFromSuperview];
  172. }];
  173. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(categoryRightView:KindFootAtIndexPath:)] && [self.dataSouce categoryRightView:self KindFootAtIndexPath:indexPath]) {
  174. UICollectionReusableView *footerView = [self.dataSouce categoryRightView:self KindFootAtIndexPath:indexPath];
  175. CGRect frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
  176. if (view.frame.size.height > sectionModel.borderInsets.bottom) {
  177. frame.size.height = view.frame.size.height-sectionModel.borderInsets.bottom;
  178. }
  179. if (sectionModel.roundModel.isCalculateHeader) {
  180. frame.origin.x = sectionModel.borderInsets.left;
  181. frame.size.width = view.frame.size.width-sectionModel.borderInsets.left-sectionModel.borderInsets.right;
  182. }
  183. footerView.frame = frame;
  184. footerView.backgroundColor = sectionModel.footerBgColor;
  185. [view addSubview:footerView];
  186. } else{
  187. view.backgroundColor = sectionModel.footerBgColor;
  188. }
  189. return view;
  190. }
  191. return nil;
  192. }
  193. #pragma mark - CGXVerticalMenuRoundFlowLayout
  194. - (UIColor *)collectionView:(UICollectionView *)categoryView BackgroundColorForSection:(NSInteger)section
  195. {
  196. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(categoryRightView:BackgroundColorForSection:)]) {
  197. return [self.dataSouce categoryRightView:self BackgroundColorForSection:section];
  198. }
  199. return self.collectionView.backgroundColor;
  200. }
  201. /// 设置底色参数
  202. /// @param collectionView collectionView description
  203. /// @param section section description
  204. - (CGXVerticalMenuRoundModel *)collectionView:(UICollectionView *)collectionView
  205. configModelForSectionAtIndex:(NSInteger)section
  206. {
  207. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  208. return sectionModel.roundModel;
  209. }
  210. /// 设置底色偏移点量(与collectionview的sectionInset用法相同,但是与sectionInset区分)
  211. /// @param collectionView collectionView description
  212. /// @param section section description
  213. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView borderEdgeInsertsForSectionAtIndex:(NSInteger)section
  214. {
  215. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  216. return sectionModel.borderInsets;
  217. }
  218. /// 设置是否计算headerview(根据section判断是否单独计算)
  219. /// @param collectionView collectionView description
  220. /// @param section section description
  221. - (BOOL)collectionView:(UICollectionView *)collectionView isCalculateHeaderViewIndex:(NSInteger)section
  222. {
  223. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  224. return sectionModel.roundModel.isCalculateHeader;
  225. }
  226. /// 设置是否计算footerview(根据section判断是否单独计算)
  227. /// @param collectionView collectionView description
  228. /// @param section section description
  229. - (BOOL)collectionView:(UICollectionView *)collectionView isCalculateFooterViewIndex:(NSInteger)section
  230. {
  231. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  232. return sectionModel.roundModel.isCalculateFooter;
  233. }
  234. /// 背景图点击事件
  235. /// @param collectionView collectionView description
  236. /// @param indexPath 点击背景图的indexPath
  237. - (void)collectionView:(UICollectionView *)collectionView didSelectDecorationViewAtIndexPath:(NSIndexPath *)indexPath
  238. {
  239. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:didSelectDecorationViewAtIndexPath:)]) {
  240. [self.delegate categoryRightView:self didSelectDecorationViewAtIndexPath:indexPath];
  241. }
  242. }
  243. /*
  244. 是否悬停
  245. */
  246. - (BOOL)collectionView:(UICollectionView *)collectionView sectionHeadersPinAtSection:(NSInteger)section
  247. {
  248. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  249. return sectionModel.headersHovering;
  250. }
  251. /*
  252. 悬停上部距离
  253. */
  254. - (CGFloat)collectionView:(UICollectionView *)collectionView sectionHeadersPinTopSpaceAtSection:(NSInteger)section
  255. {
  256. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[section];
  257. return sectionModel.headersHoveringTopEdging;
  258. }
  259. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  260. {
  261. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(categoryRightView:cellForItemAtIndexPath:)] && [self.dataSouce categoryRightView:self cellForItemAtIndexPath:indexPath]) {
  262. return [self.dataSouce categoryRightView:self cellForItemAtIndexPath:indexPath];
  263. }
  264. CGXVerticalMenuCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CGXVerticalMenuCollectionCell class]) forIndexPath:indexPath];
  265. CGXVerticalMenuCollectionSectionModel *sectionModel = self.dataArray[indexPath.section];
  266. CGXVerticalMenuCollectionItemModel *itemModel = sectionModel.rowArray[indexPath.row];
  267. [cell reloadData:itemModel];
  268. return cell;
  269. }
  270. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  271. {
  272. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:didClickSelectedItemAtIndexPath:)]) {
  273. [self.delegate categoryRightView:self didClickSelectedItemAtIndexPath:indexPath];
  274. }
  275. }
  276. #pragma mark - KVO
  277. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  278. if ([keyPath isEqualToString:@"contentOffset"]) {
  279. CGPoint contentOffset = [change[NSKeyValueChangeNewKey] CGPointValue];
  280. if ((self.collectionView.isTracking || self.collectionView.isDecelerating)) {
  281. //只处理用户滚动的情况
  282. [self contentOffsetOfContentScrollViewDidChanged:contentOffset];
  283. }
  284. }
  285. }
  286. - (void)contentOffsetOfContentScrollViewDidChanged:(CGPoint)contentOffset
  287. {
  288. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:dropUpDownDidChanged:)]) {
  289. [self.delegate categoryRightView:self dropUpDownDidChanged:contentOffset];
  290. }
  291. }
  292. // CollectionView分区标题即将展示
  293. - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
  294. {
  295. view.layer.zPosition = 0.0;
  296. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:willDisplaySupplementaryView:forElementKind:atIndexPath:)]) {
  297. [self.delegate categoryRightView:self willDisplaySupplementaryView:view forElementKind:elementKind atIndexPath:indexPath];
  298. }
  299. }
  300. // CollectionView分区标题展示结束
  301. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(nonnull UICollectionReusableView *)view forElementOfKind:(nonnull NSString *)elementKind atIndexPath:(nonnull NSIndexPath *)indexPath
  302. {
  303. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:)]) {
  304. [self.delegate categoryRightView:self didEndDisplayingSupplementaryView:view forElementOfKind:elementKind atIndexPath:indexPath];
  305. }
  306. }
  307. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  308. {
  309. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:ScrollViewDidScroll:)]) {
  310. [self.delegate categoryRightView:self ScrollViewDidScroll:scrollView];
  311. }
  312. }
  313. -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  314. {
  315. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:scrollViewWillBeginDragging:)]) {
  316. [self.delegate categoryRightView:self scrollViewWillBeginDragging:scrollView];
  317. }
  318. }
  319. -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  320. {
  321. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:scrollViewDidEndDragging:willDecelerate:)]) {
  322. [self.delegate categoryRightView:self scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
  323. }
  324. }
  325. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
  326. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:scrollViewDidEndScrollingAnimation:)]) {
  327. [self.delegate categoryRightView:self scrollViewDidEndScrollingAnimation:scrollView];
  328. }
  329. }
  330. -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  331. if (self.delegate && [self.delegate respondsToSelector:@selector(categoryRightView:scrollViewDidEndDecelerating:)]) {
  332. [self.delegate categoryRightView:self scrollViewDidEndDecelerating:scrollView];
  333. }
  334. }
  335. - (void)updateRightWithDataArray:(NSMutableArray<CGXVerticalMenuCollectionSectionModel *> *)dataArray
  336. {
  337. [self.dataArray removeAllObjects];
  338. [self.dataArray addObjectsFromArray:dataArray];
  339. [self.collectionView reloadData];
  340. }
  341. - (void)setDataSouce:(id<CGXVerticalMenuCollectionViewDataSouce>)dataSouce
  342. {
  343. _dataSouce = dataSouce;
  344. if ([self.dataSouce respondsToSelector:@selector(customcategoryRightViewCollectionViewCellClass)] && [self.dataSouce customcategoryRightViewCollectionViewCellClass]) {
  345. [self registerCell:[self.dataSouce customcategoryRightViewCollectionViewCellClass] IsXib:NO];
  346. }else if ([self.dataSouce respondsToSelector:@selector(customcategoryRightViewCollectionViewCellNib)] && [self.dataSouce customcategoryRightViewCollectionViewCellNib]) {
  347. [self registerCell:[self.dataSouce customcategoryRightViewCollectionViewCellNib] IsXib:YES];
  348. }
  349. }
  350. - (void)dealloc
  351. {
  352. if (self.collectionView) {
  353. [self.collectionView removeObserver:self forKeyPath:@"contentOffset"];
  354. }
  355. }
  356. - (void)willMoveToSuperview:(UIView *)newSuperview {
  357. [super willMoveToSuperview:newSuperview];
  358. UIResponder *next = newSuperview;
  359. while (next != nil) {
  360. if ([next isKindOfClass:[UIViewController class]]) {
  361. UIViewController *vc = (UIViewController *)next;
  362. if (@available(iOS 11.0, *)) {
  363. vc.automaticallyAdjustsScrollViewInsets = NO;
  364. }
  365. break;
  366. }
  367. next = next.nextResponder;
  368. }
  369. }
  370. - (void)registerCell:(Class)classCell IsXib:(BOOL)isXib
  371. {
  372. if (![classCell isKindOfClass:[UICollectionViewCell class]]) {
  373. NSAssert(![classCell isKindOfClass:[UICollectionViewCell class]], @"注册cell的registerCellAry数组必须是UICollectionViewCell类型");
  374. }
  375. if (isXib) {
  376. [self.collectionView registerNib:[UINib nibWithNibName:[NSString stringWithFormat:@"%@", classCell] bundle:nil] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@", classCell]];
  377. } else{
  378. [self.collectionView registerClass:classCell forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@", classCell]];
  379. }
  380. }
  381. - (void)registerFooter:(Class)footer IsXib:(BOOL)isXib
  382. {
  383. if (![footer isKindOfClass:[UICollectionReusableView class]]) {
  384. NSAssert(![footer isKindOfClass:[UICollectionReusableView class]], @"注册cell的registerCellAry数组必须是UICollectionReusableView类型");
  385. }
  386. if (isXib) {
  387. [self.collectionView registerNib:[UINib nibWithNibName:[NSString stringWithFormat:@"%@", footer] bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[NSString stringWithFormat:@"%@", footer]];
  388. } else{
  389. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[NSString stringWithFormat:@"%@", footer]];
  390. }
  391. }
  392. - (void)registerHeader:(Class)header IsXib:(BOOL)isXib
  393. {
  394. if (![header isKindOfClass:[UICollectionReusableView class]]) {
  395. NSAssert(![header isKindOfClass:[UICollectionReusableView class]], @"注册cell的registerCellAry数组必须是UICollectionReusableView类型");
  396. }
  397. if (isXib) {
  398. [self.collectionView registerNib:[UINib nibWithNibName:[NSString stringWithFormat:@"%@", header] bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[NSString stringWithFormat:@"%@", header]];
  399. } else{
  400. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[NSString stringWithFormat:@"%@", header]];
  401. }
  402. }
  403. @end