scrollView完成瀑布流:
1.根据tableView设计一个继承于scrollView的waterFlawLayoutView:
设置数据源dataSource和代理delegate方法:
1>询问数据源要数据:
//cell的对应的数据一共有多少个
-(NSInteger)numberOfCellInWaterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView;
//对应下标的cell的数据
-(LYWaterFlawVIewCell *)waterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView cellAtIndex:(NSUInteger )index;
//列数
-(NSUInteger)numberOfColumnInWaterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView;
2>代理方法
注意:@protocol LYWaterFlawLayoutViewDelegate<UIScrollView>//记得要成为scrollview的代理
-(void)waterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView didSelectedAtIndex:(NSInteger )index;
//每列的高度
- (CGFloat)waterflowView:(LYWaterFlawLayoutVIew *)waterflowView heightAtIndex:(NSUInteger)index;
//间距
- (CGFloat)waterflowView:(LYWaterFlawLayoutVIew *)waterflowView intervalForType:(LYWaterFlawLayoutVIewIntervalType)type;
//学习苹果的tableView高度是在delelgagte的,但为什么高度是放在delegate而不是在数据源方法中,why?
//接口
//刷新数据
-(void)reloadData;
//用标识符从缓冲池中换取
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
2.LYWaterFlawViewCell
//提供标识符
@property (nonatomic, copy) NSString *identifier;//标识符
3LYWaterFlawView
//保存每个cellde 大小位置
@property(nonatomic,strong)NSMutableArray *cellFrames;
//正在显示的cell
@property(nonatomic,strong)NSMutableDictionary *showingInScreenCell;
//cell的缓存池
@property (nonatomic, strong) NSMutableSet *reusableCells;
@implementation
//刷新重新计算所有cell的frame
-(void)reloadData {
/问数据源拿到总cell的数目
NSUInteger numberOfCells = [self.dataSource numberOfCellInWaterFlawView:self];
//问数据源拿到总的列数
NSUInteger numberOfColumns = [self numberOfColumns];
//所有的间距
CGFloat topInterval = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeTop]; //上
CGFloat leftInterval = [self intervalOfType:LYWaterFlawLayoutVIewInterValTypeLeft]; //左
CGFloat bottomInterval = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeBottom];//下
CGFloat rightInterval = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeRight]; //右
CGFloat columnInterval = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeColumn];//列
CGFloat rowInterval = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeRow]; //行
//cell的宽度
CGFloat cellWidth = (self.width - (numberOfColumns -1)*columnInterval - leftInterval -rightInterval) / numberOfColumns;
//C的数组用于保存每列的当前的高度
CGFloat maxY[numberOfColumns];
for (int i = 0 ; i < numberOfColumns; i++) {
maxY[i] = 0.0;
}
}
for (int i = 0 ; i < numberOfCells; i++)
{ NSUInteger minCellColumn = 0;//记录最小Y的那列 CGFloat minYOfColumns = maxY[minCellColumn];//设最小那列的y值 for (int j = 1; jcontentY) {
contentY = maxY[i];
}
网友评论