scrollView完成瀑布流:
1.根據(jù)tableView設(shè)計一個繼承于scrollView的waterFlawLayoutView:
設(shè)置數(shù)據(jù)源dataSource和代理delegate方法:
?1>詢問數(shù)據(jù)源要數(shù)據(jù):
//cell的對應的數(shù)據(jù)一共有多少個
-(NSInteger)numberOfCellInWaterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView;
//對應下標的cell的數(shù)據(jù)
-(LYWaterFlawVIewCell *)waterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView cellAtIndex:(NSUInteger )index;
//列數(shù)
-(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而不是在數(shù)據(jù)源方法中牺堰,why?
//接口
//刷新數(shù)據(jù)
-(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 {
/問數(shù)據(jù)源拿到總cell的數(shù)目
NSUInteger numberOfCells? = [self.dataSource numberOfCellInWaterFlawView:self];
//問數(shù)據(jù)源拿到總的列數(shù)
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的數(shù)組用于保存每列的當前的高度
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];//設(shè)最小那列的y值? ? ? ? ? ? ? ? for (int j = 1; jcontentY) {
contentY = maxY[i];
}