iOS自定義瀑布流

jopen 9年前發布 | 898 次閱讀 Objective-C IOS

 #define kWidth self.frame.size.width

define kHeight self.frame.size.height

@interface JRScrollView()

@property (nonatomic, strong) NSMutableArray * frameArray;

@property (nonatomic, strong) NSMutableDictionary * temDic;

@property (nonatomic, strong) NSMutableSet * temSet;

@property (nonatomic, strong) NSMutableDictionary * inDic;

@end

@implementation JRScrollView

//懶加載

  • (NSMutableArray *)frameArray

{

if (_frameArray == nil)

{

    _frameArray = [NSMutableArray array];

}

return _frameArray;

}

  • (NSMutableDictionary *)temDic

{

if (_temDic == nil)

{

    _temDic = [NSMutableDictionary dictionary];

}

return _temDic;

}

  • (NSMutableSet *)temSet

{

if (_temSet == nil)

{

    _temSet = [NSMutableSet set];

}

return _temSet;

}

  • (NSMutableDictionary *)inDic

{

if (_inDic == nil)

{

    _inDic = [NSMutableDictionary dictionary];

}

return _inDic;

}

//初始化

  • (instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame])

{

    self.showsVerticalScrollIndicator = NO;

    self.backgroundColor = [UIColor whiteColor];

}

return self;

}

//加載數據

  • (void)loadDataWithArray:(NSMutableArray *)array

{

//獲得列數

NSInteger colums = 3;

if ([self.delegateMe respondsToSelector:@selector(numberOfColums:)])

{

    colums = [self.delegateMe numberOfColums:self];

}



//獲取cell數

NSInteger count = 0;

if ([self.dataSourceMe respondsToSelector:@selector(numberOfCell:)])

{

    count = [self.dataSourceMe numberOfCell:self];

}



//計算左右間距

CGFloat marginLR = 10;



//計算上下間距

CGFloat marginUD = marginLR;



//計算cell寬度

CGFloat cellW = (kWidth-marginLR*(colums+1))/colums;



//標記每一列的做大Y值

NSMutableArray * maxYAry = [NSMutableArray array];

for (int i = 0; i<colums; i++)

{

    NSMutableDictionary * dic = [NSMutableDictionary dictionary];

    [dic setObject:@(0) forKey:@"maxY"];



    //計算放在這一列的x坐標

    CGFloat cellX = (cellW+marginLR)*i+marginLR;

    [dic setObject:@(cellX) forKey:@"cellX"];



    //添加數組

    [maxYAry addObject:dic];

}



//計算高度和坐標

for (int i = 0; i<count; i++)

{

    JRCellModel * model = array[i];



    //計算cell高度

    CGFloat cellH = model.h*cellW/model.w;


    //獲取maxY最小的一列

    [maxYAry sortUsingComparator:^NSComparisonResult(id obj1, id obj2)

    {

        return [obj1[@"maxY"] doubleValue] - [obj2[@"maxY"] doubleValue];

    }];

    NSMutableDictionary * dic = [maxYAry firstObject];



    //cell放在那一列(x值)

    CGFloat cellX = [dic[@"cellX"] doubleValue];



    //cell的y值

    CGFloat cellY = [dic[@"maxY"] intValue] + marginUD;



    //更新這個字典的屬性

    CGFloat tempY = cellY+cellH;

    [dic setObject:@(tempY) forKey:@"maxY"];



    //創建frame,加入數組

    CGRect frame = CGRectMake(cellX, cellY, cellW, cellH);

    [self.frameArray addObject:[NSValue valueWithCGRect:frame]];



    //計算contentsize

    if (i == count - 1) self.contentSize = CGSizeMake(kWidth, tempY+marginUD);



}


//布局

  • (void)layoutSubviews

{

[super layoutSubviews];



//判斷frame在不在視野中

BOOL isIn;

for (int i = 0; i<self.frameArray.count; i++)

{

    CGRect frame = [self.frameArray[i] CGRectValue];

    CGFloat offsetY = self.contentOffset.y;

    isIn = offsetY<CGRectGetMaxY(frame) && (offsetY+self.frame.size.height)>CGRectGetMinY(frame);



    //在,新添加的(原來有的不處理)

    if(isIn)

    {

        if(!self.inDic[@(i)])

        {

            //添加新的視圖

            JRCell * cell = [self.dataSourceMe scroll:self cellAtIndex:i];

            cell.frame = [self.frameArray[i] CGRectValue];

            [self addSubview:cell];



            //處理inDic字典

            [self.inDic setObject:cell forKey:@(i)];

        }

    }

    //不在,新刪除的(一直不在的不處理)

    else

    {

        JRCell * cell = self.inDic[@(i)];

        if(cell)

        {

            NSMutableSet * set = self.temDic[cell.reuseableID];

            [set addObject:cell];



            [self.inDic removeObjectForKey:@(i)];

            [cell removeFromSuperview];

        }

    }

}

}

//重復利用

  • (JRCell )dequWithReusedableID:(NSString )ID

{

NSMutableSet * set = self.temDic[ID];

if (!set)

{

    set = [NSMutableSet set];

    [self.temDic setObject:set forKey:ID];

}

// NSLog(@"%li", set.count);

JRCell * cell = [set anyObject];

if (cell)

{

    [set removeObject:cell];

}



return cell;

}

@end

</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!