开发者社区> 问答> 正文

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

#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
 

展开
收起
杨冬芳 2016-06-27 11:42:13 2935 0
1 条回答
写回答
取消 提交回答
  • IT从业

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

    2019-07-17 19:47:29
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载