3

我想知道哪种方法是为某些气泡设置最小宽度的正确方法。我已经尝试在我的自定义单元格中覆盖 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 子类中访问左右约束,但无济于事

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

4

1 回答 1

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;
}

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

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

于 2015-07-01T21:29:12.897 回答