I am having two custom UICollectionViewCell
classes, however, I want to create a variable of them with the same name in the same scope because all properties and all code in cellForItemAtIndexPath
method is same for both custom cells.
I want to do something like this: ex:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CustomCell1/CustomCell2 *cell;
if (condition) {
cell = (CustomCell1 *)[collectionView dequeueReusableCellWithReuseIdentifier:kCustomCell1Identifier forIndexPath:indexPath];
} else {
cell = (CustomCell2 *)[collectionView dequeueReusableCellWithReuseIdentifier:kCustomCell2Identifier forIndexPath:indexPath];
}
// Remaining code is same for both custom cells
cell.property1 = value1;
cell.property2 = value2;
.
.
.
.
.
//lot of code
cell.property100 = value100;
return cell;
}
My purpose behind this is that I don't want to repeat all that remaining code. I know that creating common custom cell class and writing this condition in that common class might be the solution but in that how can I pass condition variable to custom class before that class gets initialized. Because I think we can't initialize (alloc init) UICollectionViewCell.
Is this possible with the conditional(ternary) operator?