下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
#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
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的iOS自定义瀑布流全部内容,希望文章能够帮你解决iOS自定义瀑布流所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)