UIViewController不加载其UICollectionView属性

时间:2014-12-21 15:37:59

标签: ios uitabbarcontroller uicollectionview exc-bad-access

我有一个包含2个UIViewControllers的UITabBarController。当我尝试从第一个UIViewController显示时,第二个UIViewController崩溃了。 第二个UIViewController因其UICollectionView声明为私有属性而崩溃。 我得到一个EXC_BAD_ACCESS所以我认为第二个UIViewController尝试做[self setCollectionView],但是当它的属性self.collectionView还没有初始化时(仍为零)。 我不明白它为什么会这样 - 我在第一个UIViewController中以相同的方式实现UICollectionView没有问题。这是第二个UIViewController的.m文件:

@interface WorkoutViewController () <UICollectionViewDataSource, UICollectionViewDelegate, DAPageControlViewDelegate>

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) DAPageControlView *pageControlView;

@end

@implementation WorkoutViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [self createCollectionView];

    [self createPageView];

    // Constraints

    // CollectionView
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    // PageControl
    [self.pageControlView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.view);
        make.leading.equalTo(self.view);
        make.height.equalTo(screenAdjustedSizeFrom(15));
        make.bottom.equalTo(self.view).offset(screenAdjustedSizeFrom(-15).floatValue);
    }];

    // Wake up collectionView
    [self.collectionView reloadData];

}


-(void)createCollectionView {

    // Layout
    UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
    collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // UICollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    [self.view addSubview:self.collectionView];

    // Behavior
    self.collectionView.pagingEnabled = YES;

    // Appearance
    self.collectionView.backgroundColor = [Color colorWithName:nil alpha:0.2f];
    self.collectionView.showsHorizontalScrollIndicator = NO;

    // Register cells
    //[self.collectionView registerClass:[TrackingSetCollectionViewCell class] forCellWithReuseIdentifier:TrackingSetCollectionViewCellIdentifier]; }

-(void)createPageView {

    // Support for pagination - DAPageControlView
    self.pageControlView = [[DAPageControlView alloc] initWithFrame:CGRectZero];
    self.pageControlView.delegate = self;

    [self.view addSubview:self.pageControlView];
    self.pageControlView.hidden = YES; // do not show the dots
     }

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

        return fetchManagedObjectsFromEntity(@"Autor", @[[NSSortDescriptor sortDescriptorWithKey:@"autorID" ascending:YES]], nil, self.managedObjectContext).count + 1; // +1 for testing !
    }

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

        //test

        UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

        if (indexPath.row==0) {
            cell.backgroundColor = [UIColor blueColor];
        }
        if (indexPath.row==1) {
            cell.backgroundColor = [UIColor redColor];
        }

        return cell;
    }

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return self.collectionView.bounds.size;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

    return 0.0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

    return UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark - DAPageControlView Delegate

- (void)pageControlViewDidChangeCurrentPage:(DAPageControlView *)pageControlView
{
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:pageControlView.currentPage inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

1 个答案:

答案 0 :(得分:0)

问题解决了 - 我忘了为UICollectionViewCell注册一个类。我认为没有必要对'default'类UICollectionViewCell这样做,但是因为UICollectionView是以编程方式添加的,所以它是有意义的。