简体   繁体   中英

Adding Subview to UICollectionViewCell Subclass?

I seem to be having trouble adding a subview to a view within my UICollectionViewCell subclass.

I have an abstract UICollectionViewCell subclass titled MessageItem , which looks like this:

在此处输入图片说明

I've created a few classes that inherit from this (since they all use the same logic for the header and footer). However I can't seem to add any subviews into MessageItem 's blue view from within the child subclasses.

For example one of the child views is called TextItem . I'm trying to add a label to it's parent messageView (the blue view) but it only works if I do it in my UIViewController's cellForItemAtIndexPath:(NSIndexPath *)indexPath method, and not in my custom subclass.

This is how I'm trying to add it in my child subclass:

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

Note: I'm not using storyboard or xibs. Could that be the problem?

Update

This is what my MessageItem class is implemented:

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

This is what my collectionView looks like:

在此处输入图片说明

I would at least expect the color of the messageLabel to reflect the change in TextItem, but it doesn't.

Have you implement initWithCoder ?

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

I don't have all your code, but you code looks good to me. Maybe the problem was how you init the TextItem.

Here is a demo using your code, it works fine to me. https://www.dropbox.com/s/7qp9ayqnyacf57j/CustomCellView.zip?dl=0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM