我想知道哪种方法可以为某些气泡设置最小宽度。我已经尝试覆盖applyLayoutAttributes:在我的自定义单元格中,但我需要访问我的数据源才能知道哪个单元格应该具有最小宽度。我一直在修补cellForItemAtIndexPath中的messageBubbleLeftRightMargin:但没有结果。 任何指针都会很棒
修改
我的消息模型(符合)有一个标志,告诉我泡沫是否需要最小宽度
在我的自定义单元格中,我可以覆盖自定义布局,但是我无法访问数据源,因此我无法访问该标志
-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
JSQMessagesCollectionViewLayoutAttributes *customAttributes = (JSQMessagesCollectionViewLayoutAttributes *)layoutAttributes;
// I need access to my <JSQMessageData> for the condition
if (condition) {
if(customAttributes.messageBubbleContainerViewWidth <175){
customAttributes.messageBubbleContainerViewWidth = 175;
}
}
[super applyLayoutAttributes:customAttributes];
}
我也尝试在我的JSQMessagesViewController子类中访问leftright约束,但无济于事
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
/**
* Override point for customizing cells
*/
JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];
BLMessage *message = [self.visibleMessagesArray objectAtIndex:indexPath.item];
JSQMessagesCollectionViewLayoutAttributes *customAttributes = [JSQMessagesCollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
if(message.isEphemeral){
//random number to see if it had an effect
self.collectionView.collectionViewLayout.messageBubbleLeftRightMargin = 200;
//I also tried modifying the customAttributes
//customAttributes.messageBubbleContainerViewWidth = 175;
}
return cell;
}
我是UICollectionViewFlowLayout的新手,我可能会遗漏一些核心概念
答案 0 :(得分:4)
好的,所以我发现了什么是错的。我没有将新布局应用到单元格,所以我的cellForItemAtIndexPath最终结果如下:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
/**
* Override point for customizing cells
*/
JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];
BLMessage *message = [self.visibleMessagesArray objectAtIndex:indexPath.item];
JSQMessagesCollectionViewLayoutAttributes *customAttributes = (JSQMessagesCollectionViewLayoutAttributes *)[self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
if(message.isEphemeral){
//I also tried modifying the customAttributes
customAttributes.messageBubbleContainerViewWidth = 175;
[cell applyLayoutAttributes: customAttributes];
}
return cell;
}
<强> ------------- ------------ EDIT 强>
之前我的回答有一些副作用,建议不要在applyLayoutAttributes:
内拨打collectionView:cellForItemAtIndexPath:
。
执行此操作的正确方法是继承JSQMessagesCollectionViewFlowLayout
<强> CustomCollectionViewFlowLayout.h 强>
#import "JSQMessagesCollectionViewFlowLayout.h"
@interface CustomCollectionViewFlowLayout : JSQMessagesCollectionViewFlowLayout
@property (strong, nonatomic) UIImageView *incomingBubbleMask;
@end
<强> CustomCollectionViewFlowLayout.m 强>
#import "CustomCollectionViewFlowLayout.h"
#import "JSQMessage.h"
#import "JSQMessagesCollectionView.h"
#import "JSQMessagesCollectionViewCell.h"
#import "JSQMessagesCollectionViewLayoutAttributes.h"
#import "JSQMessagesCollectionViewFlowLayoutInvalidationContext.h"
#import "UIImage+JSQMessages.h"
@implementation CustomCollectionViewFlowLayout
+ (Class)layoutAttributesClass
{
return [JSQMessagesCollectionViewLayoutAttributes class];
}
- (CGSize)messageBubbleSizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize superSize = [super messageBubbleSizeForItemAtIndexPath:indexPath];
JSQMessage *currentMessage = (JSQMessage *)[self.collectionView.dataSource collectionView:self.collectionView messageDataForItemAtIndexPath:indexPath];
/*********** Setting size **************/
//check if outgoing, you can import your Session Manager to check your user identifier
if ([currentMessage.senderId isEqualToString:@"me") {
superSize = CGSizeMake(175, superSize.height);
}
//do whatever other checks and setup your width/height accordingly
return superSize;
}
@end
不要忘记在JSQMessagesViewController
子类#import "CustomCollectionViewFlowLayout.h"
和viewDidLoad
添加self.collectionView.collectionViewLayout = [[CustomCollectionViewFlowLayout alloc] init];