jessesquires / JSQMessagesViewController

An elegant messages UI library for iOS
https://www.jessesquires.com/blog/officially-deprecating-jsqmessagesviewcontroller/
Other
11.14k stars 2.81k forks source link

Cell coupled with CellIncoming and CellOutgoing #657

Closed bioshrek closed 9 years ago

bioshrek commented 9 years ago

Hi, all. This project is amazing.

My intention is adding more widgets on cell. So i subclass JSQMessagesCollectionViewCell as SKCell, adding more elements to it, and creating 2 concrete classes extending SKCell for binding xib files. When i created the xib file, i copied the JCellIncoming.xib and JCellOutgoing.xib. I also subclass JSQMessagesViewController, registered new cell prototypes, and replaced incoming/outouging cell reusable id. Aftering these steps, i could run the demo with new cell prototype, but the avatar image size was always zero regardless of the user setting, nor was the bubble size correctly calculated.

I went through the code, and i found following codes in JSQMessagesCollectionViewCell.m causing the problem:

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    ... 
    if ([self isKindOfClass:[JSQMessagesCollectionViewCellIncoming class]]) {
        self.avatarViewSize = customAttributes.incomingAvatarViewSize;
    }
    else if ([self isKindOfClass:[JSQMessagesCollectionViewCellOutgoing class]]) {
        self.avatarViewSize = customAttributes.outgoingAvatarViewSize;
    }
}

So subclass of JCell never apply avatarSize. I can subclass JCellIncoming and JCelloutgoing to solve the problem. But i don't like this solution. Because JCell is coupled with JCellIncoming and JCellOutging, what is more, extending JCell is more appropriate than extending JCellIncoming/JCellOutgoing when the newly added widget is a common element.

Thanks for your attention, and your contribution to this wonderful project. I am a fresh iOS developer. I really learn so much from your brilliant ideas and elegant codes.

jessesquires commented 9 years ago

Thanks @bioshrek ! :smile:

Did you make 2 separate custom subclasses for cells? 1 for outgoing, 1 for incoming? If so, I think this should work.

If not, then I think the best solution here is to override applyLayoutAttributes: in your custom cell class. Call [super applyLayoutAttributes:] first, then make the adjustments that you need.

bioshrek commented 9 years ago

I guess i will take the 1st solution, creating separate custom subclasses for outgoing and incoming cells. The 2nd solution requires exporting some private properties of JCell.

Thanks for your quick reply.