#import <UIKit/UIKit.h> #import "TMQuiltView.h" @interface WaterFlowVC : UIViewController<TMQuiltViewDataSource,TMQuiltViewDelegate> { TMQuiltView *_tmQuiltView; NSMutableArray *_images; } @property (nonatomic,retain)TMQuiltView *tmQuiltView; @property (nonatomic,retain)NSMutableArray *images; @end
#pragma mark - #pragma mark TMQuiltViewDataSource -(NSInteger)quiltViewNumberOfCells:(TMQuiltView *)TMQuiltView { return [self.images count]; } -(TMQuiltViewCell *)quiltView:(TMQuiltView *)quiltView cellAtIndexPath:(NSIndexPath *)indexPath { NSString *identifierStr = @"photoIdentifier"; TMPhotoQuiltViewCell *cell = (TMPhotoQuiltViewCell *)[quiltView dequeueReusableCellWithReuseIdentifier:identifierStr]; if (!cell) { cell = [[[TMPhotoQuiltViewCell alloc] initWithReuseIdentifier:identifierStr] autorelease]; } cell.photoView.image = [self imageAtIndexPath:indexPath]; cell.titleLabel.text = [NSString stringWithFormat:@"%d", indexPath.row + 1]; return cell; } #pragma mark - #pragma mark TMQuiltViewDelegate //列数 - (NSInteger)quiltViewNumberOfColumns:(TMQuiltView *)quiltView { return 2; } //单元高度 - (CGFloat)quiltView:(TMQuiltView *)quiltView heightForCellAtIndexPath:(NSIndexPath *)indexPath { float height = [self imageAtIndexPath:indexPath].size.height / [self quiltViewNumberOfColumns:quiltView]; return height; }
-(NSArray *)images和- (UIImage *)imageAtIndexPath:(NSIndexPath *)indexPath两个方法也写在了数据源中,注意这两个方法不是数据源的方法。
其中TMPhotoQuiltViewCell是我直接从demo中拷贝出来的自定义的单元,你可以根据自己的需求将其改成你想要的外观。
代理方法与tableview的代理很是相似,所以也很容易懂。
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; _tmQuiltView = [[TMQuiltView alloc] init]; _tmQuiltView.frame = CGRectMake(0, 0, 320, [[UIScreen mainScreen] bounds].size.height-20-44); _tmQuiltView.delegate = self; _tmQuiltView.dataSource = self; [self.view addSubview:_tmQuiltView]; [_tmQuiltView reloadData]; NSMutableArray *imageNames = [[NSMutableArray alloc] init]; for (int i = 0; i< kNumberOfCells;i++ ) { [imageNames addObject:[NSString stringWithFormat:@"%d.jpeg",i % 10 + 1]]; } self.images = imageNames; [imageNames release]; [_tmQuiltView reloadData]; }
//添加这句解决了阴影卡顿问题 self.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.bounds].CGPath; self.layer.shadowColor = [UIColor blackColor].CGColor; self.layer.shadowOffset = CGSizeMake(1, 1); self.layer.shadowOpacity = 0.5;