#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()<UICollectionViewDelegate,RFQuiltLayoutDelegate> {
BOOL isAnimating;
}
//@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
//@property (weak, nonatomic) UICollectionView *collectionView;
@property (nonatomic) NSMutableArray* numbers;
@property (nonatomic) NSMutableArray* numberWidths;
@property (nonatomic) NSMutableArray* numberHeights;
@end
int num = 0;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor redColor];
//RFQuiltLayout* layout = (id)[collectionView collectionViewLayout];
RFQuiltLayout *layout = [[RFQuiltLayout alloc]init];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(75,75);
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, 375, 667-20) collectionViewLayout:layout];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:collectionView];
[collectionView reloadData];
}
//数据初始化
- (void)datasInit {
num = 0;
self.numbers = [@[] mutableCopy];
self.numberWidths = @[].mutableCopy;
self.numberHeights = @[].mutableCopy;
for(; num<15; num++) {
[self.numbers addObject:@(num)];
[self.numberWidths addObject:@([self randomLength])];
[self.numberHeights addObject:@([self randomLength])];
}
}
- (void) viewDidAppear:(BOOL)animated {
//[self.collectionView reloadData];
}
////移除cell
//- (IBAction)remove:(id)sender {
//
// if (!self.numbers.count) {
// return;
// }
//
// NSArray *visibleIndexPaths = [self.collectionView indexPathsForVisibleItems];
// NSIndexPath *toRemove = [visibleIndexPaths objectAtIndex:(arc4random() % visibleIndexPaths.count)];
// [self removeIndexPath:toRemove];
//}
//
////刷新
//- (IBAction)refresh:(id)sender {
// [self datasInit];
// [self.collectionView reloadData];
//}
//
////增加
//- (IBAction)add:(id)sender {
// NSArray *visibleIndexPaths = [self.collectionView indexPathsForVisibleItems];
// if (visibleIndexPaths.count == 0) {
// [self addIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
// return;
// }
// NSUInteger middle = (NSUInteger)floor(visibleIndexPaths.count / 2);
// NSIndexPath *toAdd = [visibleIndexPaths firstObject];[visibleIndexPaths objectAtIndex:middle];
// [self addIndexPath:toAdd];
//}
- (UIColor*) colorForNumber:(NSNumber*)num {
return [UIColor colorWithHue:((19 * num.intValue) % 255)/255.f saturation:1.f brightness:1.f alpha:1.f];
}
#pragma mark - UICollectionView Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//[self removeIndexPath:indexPath];
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return self.numbers.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = [self colorForNumber:self.numbers[indexPath.row]];
UILabel* label = (id)[cell viewWithTag:5];
if(!label) label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
label.tag = 5;
label.textColor = [UIColor blackColor];
label.text = [NSString stringWithFormat:@"%@", self.numbers[indexPath.row]];
label.backgroundColor = [UIColor clearColor];
[cell addSubview:label];
return cell;
}
#pragma mark – RFQuiltLayoutDelegate
//获取宽和高
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout blockSizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row >= self.numbers.count) {
NSLog(@"Asking for index paths of non-existant cells!! %ld from %lu cells", (long)indexPath.row, (unsigned long)self.numbers.count);
}
CGFloat width = [[self.numberWidths objectAtIndex:indexPath.row] floatValue];
CGFloat height = [[self.numberHeights objectAtIndex:indexPath.row] floatValue];
return CGSizeMake(width, height);
// if (indexPath.row % 10 == 0)
// return CGSizeMake(3, 1);
// if (indexPath.row % 11 == 0)
// return CGSizeMake(2, 1);
// else if (indexPath.row % 7 == 0)
// return CGSizeMake(1, 3);
// else if (indexPath.row % 8 == 0)
// return CGSizeMake(1, 2);
// else if(indexPath.row % 11 == 0)
// return CGSizeMake(2, 2);
// if (indexPath.row == 0) return CGSizeMake(5, 5);
//
// return CGSizeMake(1, 1);
}
//传上下左右的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetsForItemAtIndexPath:(NSIndexPath *)indexPath {
return UIEdgeInsetsMake(2, 2, 2, 2);
}
#pragma mark - Helper methods
////增加cell
//- (void)addIndexPath:(NSIndexPath *)indexPath {
// if (indexPath.row > self.numbers.count) {
// return;
// }
//
// if(isAnimating) return;
// isAnimating = YES;
//
// [self.collectionView performBatchUpdates:^{
// NSInteger index = indexPath.row;
// [self.numbers insertObject:@(++num) atIndex:index];
// [self.numberWidths insertObject:@(1 + arc4random() % 3) atIndex:index];
// [self.numberHeights insertObject:@(1 + arc4random() % 3) atIndex:index];
// [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]];
// } completion:^(BOOL done) {
// isAnimating = NO;
// }];
//}
//
////移除
//- (void)removeIndexPath:(NSIndexPath *)indexPath {
// if(!self.numbers.count || indexPath.row > self.numbers.count) return;
//
// if(isAnimating) return;
// isAnimating = YES;
//
// [self.collectionView performBatchUpdates:^{
// NSInteger index = indexPath.row;
// [self.numbers removeObjectAtIndex:index];
// [self.numberWidths removeObjectAtIndex:index];
// [self.numberHeights removeObjectAtIndex:index];
// [self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]];
// } completion:^(BOOL done) {
// isAnimating = NO;
// }];
//}
- (NSUInteger)randomLength
{
// always returns a random length between 1 and 3, weighted towards lower numbers.
NSUInteger result = arc4random() % 6;
// 3/6 chance of it being 1.
if (result <= 2)
{
result = 1;
}
// 1/6 chance of it being 3.
else if (result == 5)
{
result = 3;
}
// 2/6 chance of it being 2.
else {
result = 2;
}
return result;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
就是没有设置collection view的delegate和datasource,还有layout的delegate没写,数据源没有初始化。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。