0

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?

ViruMax
  • 1,216
  • 3
  • 16
  • 41
  • Use a base class and add the common methods in it and derive the cells from that class. This will solve your problem. – Shailesh Chandavar Oct 14 '16 at 09:57
  • Subclass or protocol could work. And when I see `cell.propertyN = valueN` where N is from 0 to 100, I'd suggest to create a method that would do it with a object (NSArray ? NSDictionary) inside the cell. – Larme Oct 14 '16 at 09:57
  • @Larme I have given cell.propertyN = valueN just as an example. Actually, there is a lot of code like assigning images, hiding and showing different subviews, changing colours etc. – ViruMax Oct 14 '16 at 10:00
  • I think @ShaileshChandavar comment will help you. – Yogesh Mv Oct 14 '16 at 10:02
  • 1
    Why do you have created the same classes that have the same properties? Are these two classes identical? If it's not then please create a parent class that has common ivar and methods then create child classes inheriting the parent class – Inder Kumar Rathore Oct 14 '16 at 10:05
  • Is using the base class only solution? Isn't there any way to write variables having same name but different type in one scope like mentioned http://stackoverflow.com/questions/13657026/variables-having-same-name-but-different-type here. – ViruMax Oct 14 '16 at 10:05
  • @InderKumarRathore I have created two classes because their layout is different depending on one condition but all subviews and their values are same. I am handling their layout programmatically using Purelayout,and I have written that layout separately for each cell in their implementations. – ViruMax Oct 14 '16 at 10:10
  • @ViruMax you can use same class for two xibs – Inder Kumar Rathore Oct 15 '16 at 07:10
  • @InderKumarRathore Actually layout was created programmatically, so Shailesh's solution worked for me. – ViruMax Oct 17 '16 at 06:57

1 Answers1

4

You can create a base(parent) collectionviewCell class

@interface BaseCollectionViewCell : UICollectionViewCell 
//Add the common methods(can add common IBOulet and IBAction) and implement them.
@end

Now you can create new collection view cell which is derived from BaseCollectionViewCell as:

@interface ChildCell : BaseCollectionViewCell
//add methods which are specific to the ChildCell as the methods of    
//BaseCollectionViewCell is already available via inheritance.
@end

same way you can created another collection view cell as:

@interface ChildCell2 : BaseCollectionViewCell
//add methods which are specific to the ChildCell2 as the methods of    
//BaseCollectionViewCell is already available via inheritance.
@end

Now ChildCell and ChildCell2 gets all the methods of BaseCollectionViewCell.

Register your collection view cells to these classes along with separate reuse identifier. Use following code to dequeue the cell.

 BaseCollectionViewCell *cell;

  if (condition) {

  ChildCell *cell1 = (ChildCell *)[collectionView      
  dequeueReusableCellWithReuseIdentifier:kCustomCell1Identifier 
  forIndexPath:indexPath];
  //call cell1's specific properties if needed.
  cell = (BaseCollectionViewCell *) cell1;

  } else {

   ChildCell2 cell2 = (ChildCell2 *)[collectionView 
   dequeueReusableCellWithReuseIdentifier:kCustomCell2Identifier forIndexPath:indexPath];
  //call cell2's specific properties if needed.
  cell = (BaseCollectionViewCell *) cell2;

  }
//call common methods here.
Shailesh Chandavar
  • 1,614
  • 14
  • 20
  • Could you please also add that how the code will be in cellForItemAtIndexPath method like I have written in question? – ViruMax Oct 14 '16 at 10:17
  • 1
    BaseCollectionViewCell *cell; with it yes you can access common property in both cell , but what about others, ex childCell1 has one label named titleLabel , so cell.titleLabel is now not accessible because it is not part of ` BaseCollectionViewCell` hope you are getting my point – Prashant Tukadiya Oct 14 '16 at 10:32
  • @MikeAlterwe can cast the cell to get cell's specific properties. I have added the comment. – Shailesh Chandavar Oct 14 '16 at 10:33