我想知道为某些气泡设置最小宽度的正确方法是什么。我尝试在我的自定义单元格中覆盖 applyLayoutAttributes:,但我需要访问我的数据源才能知道哪个单元格应该具有最小宽度。我一直在修改 cellForItemAtIndexPath: 中的 messageBubbleLeftRightMargin: 但没有结果。
任何指针都会很棒

编辑

我的 Message 模型(符合 )有一个标志,告诉我气泡是否需要具有最小宽度

在我的自定义单元格中,我可以覆盖自定义布局,但我无权访问数据源,因此我无权访问该标志

-(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 有点陌生,我可能会遗漏一些核心概念

最佳答案

好的,所以我发现出了什么问题。我没有将新布局应用于单元格,所以我的 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;
}

-------------编辑------------

我之前的回答有一些副作用,不建议在 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];

关于ios - 设置一些气泡的最小宽度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31151335/

10-12 14:39