自定义cell时候不显示的问题,求解?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

自定义cell时候不显示的问题,求解?

2016-06-27 11:42:13 2412 1
#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
 
取消 提交回答
全部回答(1)
  • 杨冬芳
    2019-07-17 19:47:29

    就是没有设置collection view的delegate和datasource,还有layout的delegate没写,数据源没有初始化。

    0 0
相关问答

1

回答

在shell中,什么是定义数组?

2021-10-09 15:39:19 251浏览量 回答数 1

1

回答

.net托管内存如何处理对象内部的值类型?

2020-01-03 21:27:49 612浏览量 回答数 1

1

回答

cdn自定义端口

2018-10-10 13:44:47 1159浏览量 回答数 1

0

回答

xshell连接不上阿里云服务器 显示22端口被拒

2017-08-02 18:49:34 2574浏览量 回答数 0

1

回答

http://www.weizixun.net/wdy/

2016-12-16 08:29:23 1787浏览量 回答数 1

1

回答

centos自定义去监控使用shell脚本

2016-06-02 13:20:57 3277浏览量 回答数 1

3

回答

centos 7服务器静态网页发布显示404

2016-04-30 22:17:11 8811浏览量 回答数 3

1

回答

安卓,fragmentTabHost中,使用自定义的view后就出错了

2016-03-20 11:49:58 2526浏览量 回答数 1

1

回答

C++中,哪些情况需要自定义拷贝控制操作?

2016-03-06 10:07:57 1522浏览量 回答数 1

1

回答

ECSwindows2008进入远程服务器老是显示授权失败

2015-08-24 23:56:31 3844浏览量 回答数 1
+关注
杨冬芳
IT从业
1
文章
9167
问答
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载