RADetailActionCollectionModel.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // RADetailActionCollectionModel.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/2.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RADetailActionCollectionModel.h"
  9. #import "RADetailActionModel.h"
  10. @interface RADetailActionCollectionModel ()
  11. @property (nonatomic,assign) CGFloat itemSpacing;
  12. @property (nonatomic,assign) CGFloat itemHeight;
  13. @property (nonatomic,assign) NSInteger numberOfColumns;
  14. @property (nonatomic,assign) NSInteger itemCount;
  15. @property (nonatomic,assign) CGFloat collectionViewWidth;
  16. @property (nonatomic,assign) CGFloat contentHeight;
  17. @property (nonatomic,strong) NSArray *layoutAttrs;
  18. @end
  19. @implementation RADetailActionCollectionModel
  20. - (instancetype)init {
  21. if (self = [super init]) {
  22. _itemSpacing = 10.0f;
  23. _itemHeight = 30.0f;
  24. _numberOfColumns = 2;
  25. }
  26. return self;
  27. }
  28. - (void)setActions:(NSArray *)actions {
  29. NSMutableArray *modelArr = [NSMutableArray array];
  30. for (int i = 0; i < actions.count; i++) {
  31. NSDictionary *action = [actions objectAtIndex:i];
  32. RADetailActionModel *model = [[RADetailActionModel alloc] init];
  33. [model setValuesForKeysWithDictionary:action];
  34. [modelArr addObject:model];
  35. }
  36. _actions = modelArr;
  37. self.itemCount = _actions.count;
  38. self.layoutAttrs = nil;
  39. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  40. [self prepareLayout];
  41. });
  42. }
  43. - (NSInteger)actionCount {
  44. return self.actions.count;
  45. }
  46. - (RADetailActionModel *)actionModelForIndexPath:(NSIndexPath *)indexPath {
  47. return [self.actions objectAtIndex:indexPath.item];
  48. }
  49. - (CGFloat)width {
  50. CGFloat width = [super width];
  51. if (width <= 0) {
  52. NSLog(@"action width is zero");
  53. width = CGRectGetWidth([UIScreen mainScreen].bounds);
  54. self.width = width;
  55. }
  56. return width;
  57. }
  58. - (CGFloat)height {
  59. if (self.contentHeight <= 0) {
  60. [self prepareLayout];
  61. }
  62. return self.contentHeight;
  63. }
  64. - (NSArray *)prepareLayout {
  65. if (self.layoutAttrs) {
  66. return self.layoutAttrs;
  67. }
  68. self.collectionViewWidth = self.width;
  69. NSUInteger numberOfItem = self.itemCount;
  70. NSMutableArray *attrsArr = [NSMutableArray arrayWithCapacity:numberOfItem];
  71. CGFloat itemWidth = (self.collectionViewWidth - self.itemSpacing * (self.numberOfColumns + 1)) / self.numberOfColumns;
  72. for (int i = 0; i < numberOfItem; i++) {
  73. int col = i % self.numberOfColumns;
  74. int row = i / self.numberOfColumns;
  75. CGFloat x = self.itemSpacing + (itemWidth + self.itemSpacing) * col;
  76. CGFloat y = self.itemSpacing + (self.itemHeight + self.itemSpacing) * row;
  77. if (i == numberOfItem - 1 && col == 0) {
  78. itemWidth = self.collectionViewWidth - self.itemSpacing * 2;
  79. }
  80. CGRect frame = CGRectMake(x, y, itemWidth, self.itemHeight);
  81. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  82. UICollectionViewLayoutAttributes *itemAttrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  83. itemAttrs.frame = frame;
  84. [attrsArr addObject:itemAttrs];
  85. self.contentHeight = CGRectGetMaxY(frame);
  86. }
  87. self.layoutAttrs = attrsArr;
  88. self.contentHeight += self.itemSpacing;
  89. return self.layoutAttrs;
  90. }
  91. - (CGFloat)layoutContentHeight {
  92. return self.contentHeight;
  93. }
  94. @end