| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- //
- // RADetailActionCollectionModel.m
- // Apex And Drivers
- //
- // Created by Jack on 2018/6/2.
- // Copyright © 2018年 USAI. All rights reserved.
- //
- #import "RADetailActionCollectionModel.h"
- #import "RADetailActionModel.h"
- @interface RADetailActionCollectionModel ()
- @property (nonatomic,assign) CGFloat itemSpacing;
- @property (nonatomic,assign) CGFloat itemHeight;
- @property (nonatomic,assign) NSInteger numberOfColumns;
- @property (nonatomic,assign) NSInteger itemCount;
- @property (nonatomic,assign) CGFloat collectionViewWidth;
- @property (nonatomic,assign) CGFloat contentHeight;
- @property (nonatomic,strong) NSArray *layoutAttrs;
- @end
- @implementation RADetailActionCollectionModel
- - (instancetype)init {
- if (self = [super init]) {
-
- _itemSpacing = 10.0f;
- _itemHeight = 30.0f;
- _numberOfColumns = 2;
-
- }
- return self;
- }
- - (void)setActions:(NSArray *)actions {
-
- NSMutableArray *modelArr = [NSMutableArray array];
- for (int i = 0; i < actions.count; i++) {
- NSDictionary *action = [actions objectAtIndex:i];
- RADetailActionModel *model = [[RADetailActionModel alloc] init];
- [model setValuesForKeysWithDictionary:action];
- [modelArr addObject:model];
- }
-
- _actions = modelArr;
- self.itemCount = _actions.count;
- self.layoutAttrs = nil;
-
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- [self prepareLayout];
- });
- }
- - (NSInteger)actionCount {
- return self.actions.count;
- }
- - (RADetailActionModel *)actionModelForIndexPath:(NSIndexPath *)indexPath {
- return [self.actions objectAtIndex:indexPath.item];
- }
- - (CGFloat)width {
- CGFloat width = [super width];
- if (width <= 0) {
- NSLog(@"action width is zero");
- width = CGRectGetWidth([UIScreen mainScreen].bounds);
- self.width = width;
- }
- return width;
- }
- - (CGFloat)height {
- if (self.contentHeight <= 0) {
- [self prepareLayout];
- }
- return self.contentHeight;
- }
- - (NSArray *)prepareLayout {
-
- if (self.layoutAttrs) {
- return self.layoutAttrs;
- }
-
- self.collectionViewWidth = self.width;
-
- NSUInteger numberOfItem = self.itemCount;
- NSMutableArray *attrsArr = [NSMutableArray arrayWithCapacity:numberOfItem];
-
- CGFloat itemWidth = (self.collectionViewWidth - self.itemSpacing * (self.numberOfColumns + 1)) / self.numberOfColumns;
- for (int i = 0; i < numberOfItem; i++) {
-
- int col = i % self.numberOfColumns;
- int row = i / self.numberOfColumns;
-
- CGFloat x = self.itemSpacing + (itemWidth + self.itemSpacing) * col;
- CGFloat y = self.itemSpacing + (self.itemHeight + self.itemSpacing) * row;
-
-
- if (i == numberOfItem - 1 && col == 0) {
- itemWidth = self.collectionViewWidth - self.itemSpacing * 2;
- }
- CGRect frame = CGRectMake(x, y, itemWidth, self.itemHeight);
-
- NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
- UICollectionViewLayoutAttributes *itemAttrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
- itemAttrs.frame = frame;
- [attrsArr addObject:itemAttrs];
-
- self.contentHeight = CGRectGetMaxY(frame);
- }
- self.layoutAttrs = attrsArr;
- self.contentHeight += self.itemSpacing;
-
- return self.layoutAttrs;
- }
- - (CGFloat)layoutContentHeight {
- return self.contentHeight;
- }
- @end
|