我发现最好的方法是子类化JSQMessagesCollectionViewCellIncoming
和JSQMessagesCollectionViewCellOutgoing
. 这非常重要,因为库需要其中一种类型,如果JSQMessagesCollectionViewCell
直接从子类化就会遇到麻烦。顺便说一句,我复制了现有的JSQMessagesCollectionViewCellOutgoing.xib
,JSQMessagesCollectionViewCellIncoming.xib
并更改/重命名了所有内容,这使我更容易开始自定义单元格。
然后在您的 JSQMessagesViewController 子类中,在 viewDidLoad() 中注册单元标识符,如下所示:
self.outgoingCellIdentifier = [CustomCollectionViewCellOutgoing cellReuseIdentifier];
self.outgoingMediaCellIdentifier = [CustomCollectionViewCellOutgoing mediaCellReuseIdentifier];
[self.collectionView registerNib:[CustomCollectionViewCellOutgoing nib] forCellWithReuseIdentifier:self.outgoingCellIdentifier];
[self.collectionView registerNib:[CustomCollectionViewCellOutgoing nib] forCellWithReuseIdentifier:self.outgoingMediaCellIdentifier];
self.incomingCellIdentifier = [CustomCollectionViewCellIncoming cellReuseIdentifier];
self.incomingMediaCellIdentifier = [CustomCollectionViewCellIncoming mediaCellReuseIdentifier];
[self.collectionView registerNib:[CustomCollectionViewCellIncoming nib] forCellWithReuseIdentifier:self.incomingCellIdentifier];
然后在 collectionView:cellForItemAtIndexPath: 您可以访问您的自定义单元格:
[self.collectionView registerNib:[CustomCollectionViewCellIncoming nib] forCellWithReuseIdentifier:self.incomingMediaCellIdentifier];
答案来源链接:https ://github.com/jessesquires/JSQMessagesViewController/issues/1233