将子视图添加到UICollectionViewCell子类?

时间:2016-03-16 22:02:23

标签: ios objective-c autolayout uicollectionview uicollectionviewcell

我似乎无法将子视图添加到UICollectionViewCell子类中的视图中。

我有一个名为UICollectionViewCell的抽象MessageItem子类,如下所示:

enter image description here

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

例如,其中一个子视图称为TextItem。我尝试在其父级messageView(蓝色视图)中添加标签,但只有在我的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或xib。这可能是问题吗?

更新

这是我的MessageItem类的实现:

MessageItem.h

#import <UIKit/UIKit.h>

@class Message;

@interface MessageItem : UICollectionViewCell

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

@end

MessageItem.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];
}

TextItem.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的颜色反映TextItem的变化,但它没有。

1 个答案:

答案 0 :(得分:2)

您是否实施了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