I wonder if it is possible to use UICollectionViewCell with IBOutlet? As I always get this error
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the cell returned from -collectionView:cellForItemAtIndexPath: was not retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath: at index path
Below is my code:
@property (nonatomic, strong) NSMutableArray *cellArray;
@property (nonatomic, strong) IBOutlet UICollectionViewCell *sort1Cell;
@property (nonatomic, strong) IBOutlet UICollectionViewCell *sort2Cell;
@property (nonatomic, strong) IBOutlet UICollectionViewCell *sort3Cell;
@property (nonatomic, strong) IBOutlet UICollectionViewCell *sort4Cell;
- (void)viewDidLoad {
[super viewDidLoad];
self.cellArray = [NSMutableArray array];
[cellArray addObject:sort1Cell];
[cellArray addObject:sort2Cell];
[cellArray addObject:sort3Cell];
[cellArray addObject:sort4Cell];
[theCollectionView reloadData];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [cellArray objectAtIndex:indexPath.row];
return cell; //THE ERROR IS FROM HERE
}
Or perhaps I am missing something?
I wonder if it is possible to use UICollectionViewCell with IBOutlet
No, it isn't possible. (And even if it is, don't do it.) Cells are reused; they have no permanent existence within the collection view. You never know what cell object will be used at a given section/item position. Therefore you must dequeue the cell every time cellForItemAtIndexPath
is called.
There are ways to obtain a reference to a cell that actually exists within the collection view, at runtime. (Though in many case, the need to do even this is to be regarded as a Bad Smell.)