UICollectionView异常崩溃

时间:2013-08-13 19:32:24

标签: ios objective-c datasource uicollectionview

UICollectionView我有一个神秘的问题。

我将我的集合视图添加到我的tableview单元格中,如下所示:

UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
                layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

                UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100) collectionViewLayout:layout];
                [collection setDataSource:self];
                [collection setDelegate:self];
                [collection setBackgroundColor:[UIColor whiteColor]];
                [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
                collection.tag = kTagPhotoCollectionView;
                [cell.contentView addSubview:collection];

我正在实现所有必要的委托和数据源方法,你可以看到我已经设置了数据源和委托,并在我的标题中实现了它们。

当我按下显示UIButton的{​​{1}}时,该应用因此错误而崩溃:

UIImagePickerController

我已经设置了数据源,所以我不知道为什么会这样。

1 个答案:

答案 0 :(得分:5)

我遇到了类似的问题,但在我以编程方式弹出我的UICollectionViewController后,我的应用程序崩溃了。出于某种原因(我认为它只是SDK中的一个错误)self.collectionView在它的'之后仍然存在。控制器销毁,从而导致这种失败:

*** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:1305
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'

解决方案只是在UICollectionViewController中覆盖-dealloc并手动释放self.collectionView。 ARC代码:

- (void)dealloc {

    self.collectionView = nil;
}

希望这会为某人节省时间。