0

I'm creating a custom collectionViewCell called MessageCell. This message cell has three components, a headerLabel, messageContainerView, and footerLabel. The problem is that depending on the type of message (video, transaction, delivery confirmation, photo, text, etc) I want to display a specific type of view with specific actions etc.

enter image description here

What's the best way to accomplish this? I've tried setting up my container view as a UIView in my cell subclass, and depending on the type of message, set it equal to a specific subview but that isn't working:

- (void)setMessage:(EMKMessage *)message {

    //Set Message
    _message = message;

    //Check Message Type
    switch (message.type) {
        case MessageTypeText:
        default: {

            //Create Message Content View
            TextContentView *textContentView = [[TextContentView alloc] initForAutoLayout];
            textContentView.frame = CGRectMake(0, 0, 300, 200);
            [textContentView setText:message.text];
            self.messageContainerView = textContentView;

            break;
        }
    }
}

Any help would be greatly appreciated.

KingPolygon
  • 4,753
  • 7
  • 43
  • 72

1 Answers1

0

You can create separately all the cells you need. In

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

you can renturn different cells depending on your needs. Just check the type of object you want to represent at the indexPath and return the corresponding cell. Those cell can have delegates if you need to interract with them or you can use block properties. Something like:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     if (indexPath.row == 0) {
          VideoMessageCell *cell = [tableView.dequeueReusableCellWithIdentifier:@"VideoMessageCell"];
          //set the cell properties
          return cell;
     } else if (indexPath.row == 1) {
          AudioMessageCell *cell = [tableView.dequeueReusableCellWithIdentifier:@"AudioMessageCell"];
          //set the cell properties
          return cell;
     }
}

Now, I don't know how you decide which type of cell do you need for a given index but you can replace indexPath.row to suit your needs. Also do not forget to set reusable identifiers accordingly.

Jelly
  • 4,522
  • 6
  • 26
  • 42
  • Thanks Jelly. There's only one problem. I really should have mentioned this, but right now I have two cells that inherit from the `MessageCell` class, one which handles incoming messages (`IncomingMessageCell`), and another responsible for outgoing messages (`OutgoingMessageCell`). The difference between the two is the constraints and positioning. I originally planned what you're suggesting, but that would result in many duplicate cells for both IncomingMessageCell and OutgoingMessageCell. Is this really the only way? – KingPolygon Mar 23 '16 at 12:03
  • Well, the same logic could be applied even if you use inheritance, it doesn't really matter. If you need `CellInheritedClass1`, dequeue a cell with it's identifier and use it, if you need `CellInheritedClass2`, dequeue a cell of that type and use it. Just make sure you have different reuse identifiers for them. – Jelly Mar 23 '16 at 12:20
  • I'm a bit confused Jelly. Would I need create a subclass (say `VideoMessageCell` twice, one for `IncomingMessageCell` and one for `OutgoingMessageCell` or can I somehow get away with just making it once. An example would be useful. – KingPolygon Mar 23 '16 at 22:04
  • Thanks Jelly but I already know how to do this. My question has to do more with how to design my classes so that I don't have to design an audio cell twice (one for incoming messages and one for outgoing messages). – KingPolygon Mar 24 '16 at 07:29
  • This depends on your needs. You can have a base class with common views and probably some empty containers, in which subclasses will put the content. Or you can have all the views in the base class and each subclass modify the views and the constraints as needed. There is no universal solution for your problem. I would go with the first suggestion most of the time. Basically you need a way to split the views conveniently. Also you could also create some custom views and use them in your cells. It really depends on your needs. – Jelly Mar 24 '16 at 07:35