gpt4 book ai didi

ios - 设置一些气泡的最小宽度

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:56 24 4
gpt4 key购买 nike

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

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

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

我之前的回答有一些副作用,不建议在 collectionView:cellForItemAtIndexPath: 中调用 applyLayoutAttributes:

正确的做法是继承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 分配] 初始化];

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com