gpt4 book ai didi

ios - 将 subview 添加到 UICollectionViewCell 子类?

转载 作者:行者123 更新时间:2023-12-01 20:18:49 25 4
gpt4 key购买 nike

我似乎无法将 subview 添加到我的 UICollectionViewCell 中的 View 中。子类。

我有一个摘要 UICollectionViewCell标题为 MessageItem 的子类,看起来像这样:

enter image description here

我创建了一些继承自它的类(因为它们都对页眉和页脚使用相同的逻辑)。但是我似乎无法将任何 subview 添加到 MessageItem子类中的蓝色 View 。

例如,其中一个 subview 称为 TextItem .我正在尝试将标签添加到它的父 messageView(蓝色 View ),但只有在我的 UIViewController 的 cellForItemAtIndexPath:(NSIndexPath *)indexPath 中这样做才有效方法,而不是在我的自定义子类中。

这就是我试图将它添加到我的子类中的方式:

- (instancetype)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {

//Setup Message Label
[self setupMessageLabel];
}

return self;
}

#pragma mark - Setup Methods

- (void)setupMessageLabel {

NSLog(@"Setting up label");

//Setup Message Label
self.messageLabel = [TTTAttributedLabel new];
self.messageLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
self.messageLabel.textInsets = UIEdgeInsetsMake(8, 8, 8, 8);
self.messageLabel.numberOfLines = 0;
[self.messageContentView addSubview:self.messageLabel];
[self.messageContentView autoPinEdgesToSuperviewEdges];

//Update Label Color
self.messageLabel.backgroundColor = FlatRed;
}

注意:我没有使用 Storyboard或 xibs。这可能是问题吗?

更新

这就是我的 MessageItem类实现:

消息项.h
#import <UIKit/UIKit.h>

@class Message;

@interface MessageItem : UICollectionViewCell

@property (nonatomic, strong) Message *message;
@property (nonatomic, strong) UIView *messageContentView;

@end

消息项.m
@interface MessageItem ()

@property (nonatomic, strong) TTTAttributedLabel *headerLabel;
@property (nonatomic, strong) TTTAttributedLabel *footerLabel;

@end

@implementation MessageItem
@synthesize message = _message;

- (instancetype)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {

//Setup Main View
[self setupMainView];
}

return self;
}

#pragma mark - Setup Methods

- (void)setupMainView {

//Setup Header
[self setupHeaderLabel];

//Setup Message
[self setupMessageView];

//Setup Footer View
[self setupFooterLabel];
}

- (void)setupHeaderLabel {

//Setup Header Label
self.headerLabel = [[TTTAttributedLabel alloc] initForAutoLayout];
self.headerLabel.font = [UIFont fontWithName:@"Lato-Bold" size:12.0];
self.headerLabel.textColor = FlatGray;
self.headerLabel.textAlignment = NSTextAlignmentCenter;
self.headerLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
self.headerLabel.textInsets = UIEdgeInsetsMake(0, 8, 0, 8);
self.headerLabel.backgroundColor = FlatPurple;
[self.contentView addSubview:self.headerLabel];
[self.headerLabel autoSetDimension:ALDimensionHeight toSize:20.0];
[self.headerLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeBottom];
}

- (void)setupMessageView {

//Setup Message View
self.messageContentView = [UIView new];
self.messageContentView.backgroundColor = [UIColor blueColor];
[self.contentView addSubview:self.messageContentView];
[self.messageContentView autoSetDimension:ALDimensionHeight toSize:30 relation:NSLayoutRelationGreaterThanOrEqual];
[self.messageContentView autoPinEdgeToSuperviewEdge:ALEdgeLeading];
[self.messageContentView autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
[self.messageContentView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.headerLabel];
}

- (void)setupFooterLabel {

//Setup Footer Label
self.footerLabel = [[TTTAttributedLabel alloc] initForAutoLayout];
self.footerLabel.font = [UIFont fontWithName:@"Lato-Bold" size:10.0];
self.footerLabel.textColor = FlatGray;
self.footerLabel.backgroundColor = FlatGreen;
self.footerLabel.textAlignment = NSTextAlignmentLeft;
self.footerLabel.textInsets = UIEdgeInsetsMake(0, 8, 0, 8);
[self.contentView addSubview:self.footerLabel];
[self.footerLabel autoSetDimension:ALDimensionHeight toSize:10.0];
[self.footerLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading];
[self.footerLabel autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
[self.footerLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom];
[self.footerLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.messageContentView];
}

文本项.m
- (instancetype)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {

//Setup Message Label
[self setupMessageLabel];
}

return self;
}


#pragma mark - Setup Methods

- (void)setupMessageLabel {

//Setup Message Label
self.messageLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
self.messageLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
self.messageLabel.textInsets = UIEdgeInsetsMake(8, 8, 8, 8);
self.messageLabel.numberOfLines = 0;
[self.messageContentView addSubview:self.messageLabel];

//Update Label Color
self.messageLabel.backgroundColor = FlatRed;
}

#pragma mark - Setter Methods

- (void)setMessageText:(NSString *)text {

//Incoming Text Message
NSMutableAttributedString *textString = [[NSMutableAttributedString alloc] initWithString:text];
[textString addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, textString.length)];
[textString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16 weight:UIFontWeightLight] range:NSMakeRange(0, textString.length)];

//Set Paragraph Style
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.minimumLineHeight = 20;
paragraphStyle.maximumLineHeight = 20;
[textString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, textString.length)];

//Update Message Label
[self.messageLabel setText:textString];

NSLog(@"Set Message Label Text");
}

- (void)setMessage:(Message *)message {

//Super
[super setMessage:message];

//Update Message Text
[self setMessageText:message.text];
}

这就是我的 collectionView 的样子:

enter image description here

我至少希望 messageLabel 的颜色能够反射(reflect) TextItem 的变化,但事实并非如此。

最佳答案

有没有实现initWithCoder ?

- (id)initWithCoder:(NSCoder*)aDecoder 
{
if(self = [super initWithCoder:aDecoder]) {
// Do something
}
return self;
}

我没有你所有的代码,但你的代码对我来说看起来不错。也许问题在于您如何初始化 TextItem。

这是使用您的代码的演示,对我来说效果很好。 https://www.dropbox.com/s/7qp9ayqnyacf57j/CustomCellView.zip?dl=0

关于ios - 将 subview 添加到 UICollectionViewCell 子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36047794/

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